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."
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.
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
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!
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.