Problem with if statement comparison

Hello, I am using linux fedora and I am compiling with g++. I am trying to write a postfix calculator program and I am stuck on one part in my code. I enter "2 1 +" and using GDB I figured out that when the '+' is being compared in the if statement, it is not skipping over the if statement, instead the program enters into the if statement and converts the '+' to a 0 and then pushes it into my stack, nums.

Here is my code


#include <stdlib.h>
#include <stack>
#include <string>
#include <iostream>
using namespace std;

int main(void)
{
int sum, x, y;
int unsigned i;
string input;
stack<int> nums;
while(!cin.eof()){
getline(cin,input);
for(i=0;i<=input.length();i++){
if(input[i]!=('+'||'-'||'*'||'/')&&(!isspace(input[i]))) //error occurs here
nums.push(atoi(&input[i]));
switch(input[i]) {
case '+':
if(nums.size()<2)
break;
x=nums.top();
nums.pop();
y=nums.top();
nums.pop();
sum=x+y;
nums.push(sum);
break;
default:
break;
}
}
}
cout << sum << endl;
return 0;
}
You are comparing input[i] to 1 (aka true). Try this cout << ('+'||'-'||'*'||'/'); This shows how that expression is evaluated. Remember that any value that can be converted to an integer can be use in relational operators. In this case, characters like '+' evaluates to true.
Ah I see what I did. Thank you.
Topic archived. No new replies allowed.