Switch Statement

So, I have a stack. And I'm using it to convert a normal mathematical expression into a postfix expression.

And I'm using a switch statement to tell the computer what to do for every character it encounters in the "normal" expression.

One "case" of my switch statement is if the character is an operand (a number 1 through 9).

How do I implement that into my switch statement without making nine separate cases? It seems that switch statement don't allow for multiple cases to perform the same code, unless you copy paste it over again.

In other words, how do I say to the computer "perform this line of code if the character is the number 1, or the number 2, or the number 3, or the number 4, etc."

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch ( c )
{
   case '1': 
   case '2': 
   case '3': 
   case '4': 
   case '5': 
   case '6': 
   case '7': 
   case '8': 
   case '9':
      std::cout << "It is a digit" << sts::endl;
      break;
   default:
      std::cout << "You should enter a digit" << std::endl;
      break;
}  



But it is better to define an enumeration with enumerators denote each type of tokens.
Last edited on
Thanks.

But can you explain this line?
"But it is better to define an enumeration with enumerators denote each type of tokens"


An enumeration? Enumerators? Tokens?
an enum is so you can input numbers in a more user readable way with a default starting value of 0 like this
1
2
3
4
5
enum { blue , white , red , black };
std::cout << blue << std::endl;
std::cout << white << std::endl;
std::cout << red << std::endl;
std::cout << black << std::endl;

you can also give the enum a name like enum colors{ blue , white , red , black }; not sure if it is better to do that or not but I don't tbh. probably best to name it so you do not have a problem with two of the same names.
In this kind of case I would probably handle the digits in the default handler, along these lines (of course, this accepts 0 as well as 1-9)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch( c )
{
    case '+': /* todo - handle add       */ break;
    case '-': /* todo - handle take away */ break;
    case '*': /* todo - handle times     */ break;
    case '/': /* todo - handle divide by */ break;
    // etc
    default:
        if( isdigit(c) )
            cout << "It is a digit" << endl;
        else
            cout << "Invalid character" << endl;
        break;
    }
}


By the way, can your convertor only handle single digit numbers, or does it have to handle longer numbers like 180 ?

Andy
Last edited on
Well if he is converting above 9 I hope that c is a char in a string or char array =p but probably could be done much better ways. Ps I need a hero a little suggestion maybe have the user input the first number , then input the second number or sign ( + - * / ) and which ever one you don't do for the second one do for the third. ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
signed long input1 = 1, input2 = 1; //default
unsigned char sign = '+'; //default
std::cout << "Please enter number 1\n> " << std::flush;
std::cin >> input1;
std::cout << "Please enter the sign( + - * / )\n> " << std::flush;
std::cin >> sign;
std::cout << "Please enter number 2\n> " << std::flush;
std::cin >> input2;

switch(sign)
{
case '+':
    std::cout << input1 << " + " << input2 << " = " << input1 + input2 << std::endl;
    break;
case '-';
    std::cout << input1 << " - " << input2 << " = " << input1 - input2 << std::endl;
    break;
case '*':
    std::cout << input1 << " * " << input2 << " = " << input1 * input2 << std::endl;
    break;
case '/':
    std::cout << input1 << " / " << input2 << " = " << input1 / input2 << std::endl;
    break;
}

This program though has no error checking and does not using doubles since it is just an example
Last edited on
Hey guys thanks for the help!
@andy, my program (thankfully) only needs to handle single digit numbers.


The program really ought to ask the user to just enter the whole expression and not be bothered by having to type it in piecemeal.

I think the best idea is to just use the numbers in the default like andy recommended - and yes, zero ought to be included as an integer, so that isn't a problem.

My 'c' is a character array. I presume the function isDigit() tells whether a character is an integer? If so, that's a very useful function to know!

Thanks a lot everyone!
Last edited on
Strictly speaking, isdigit() -- no capital D -- says whether the char is a digit (like it's name). It's part of the C standard library, along with isspace(), isalpha(), etc.

isdigit
http://www.cplusplus.com/reference/cctype/isdigit/
Checks whether c is a decimal digit character.

Andy
Last edited on
Topic archived. No new replies allowed.