I'm trying to make a postfix/RPN calculator, and everything is working except for one little thing. If I take an input of something like "5 3f +" it returns 8, but what I want it to do is say there is a syntax error because of the f directly following the 3. To check for this, I tried to make a method that would take the whole input string, turn each number into a char array, and then search those for arrays for anything besides a number or operator.
The idea I had was to add to a counter if anything besides a number or operator is found. If the counter != 0, then the method would return false, and it would tell the user there is an operator error.
_input is already a string type. If you're trying to pass over each character in _input and find a non-integer, do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <ctype.h> //gives you isdigit() function which tests if a char is a digit.
//...
for(int i = 0; i< _input.size(); i++)
{
if( !(isdigit(_input[i])) && (_input[i] != '+') && (_input[i] != '-') && (_input[i] != '*') && (_input[i] != '/') )
counter++
//You dont need counter = counter + 0 since it doesn't change counter value.
//If you're not changing counter value, no point in the else statement in for loop.
}
//...
counter=counter + 0 means don't change counter. You don't need an instruction to not change something. So get rid of this since it doesn't change anything:
They are ugly, error prone, non-scalable and did I mention UGLY :+D
Instead it would be much better to learn how to use a switch or an if - elseif - else chain.
Now for some comedy:+)
Imagine you work in industry, you get a carpet call from your boss. While standing at attention on the carpet in front of his/her desk, the boss is asking wtf is that? The boss might even threaten to apply his /her boot to your rear end, in order for you to enter orbit around Jupiter !