Flagging specific number in postfix calculator

Hi Everyone,
I'm having a problem with displaying an error for my postfix calculator. If an input were to be something like:
1.2.3 +
Then it should cerr that it is an invalid expression. But I'm having trouble with the code that would determine if it was an error. All of my functions work and the rest of the calculator works as well. It's just this minor detail that I can't figure out. What I have for this part so far is:

1
2
3
4
5
6
7
8
9
10
if(isdigit(cin.peek()) || cin.peek() == '.')
{
   if(cin.good() == false)
   {
      error();
   }
   cin>> value;
   stack.push(value);
}
Last edited on
I would check the input before inserting it into the stack.
1
2
3
4
5
6
7
8
9
10
bool isValidChar(char ch)
{
  // check her if ch is valid
}
char ch;
cin >> ch;
if (!isValidChar(ch))
  cerr << "Error: illegal char " << ch;
else
  stack.push(ch);
Is there a way to just check the digit and output an error if it has more than one decimal and isn't separated by a space? I'm having trouble understanding why I would need to use char for anything other than my operators.
Last edited on
I am not sure how your program works. One way would be to read thow whole input into a string with getline. The you can iterate through the whole string and check if it contains any invalid input.
Maybe you also can tell us what valid input is.
Topic archived. No new replies allowed.