Weird thing happening with my code

I’m trying to solve this easy problem from SPOJ.


Here is my code : (My logic might be kind of weird)

The weird thing is that,I have commented out a cout line.If I uncomment that line,I get the right answer but commenting it gives the wrong answer.How is this even possible? Its just a printing statement.
I don’t understand.
EDIT:Using scanf("%c",&str); doesn’t work either.

here is your updated code :slight_smile:

or

u can also do :

store the complete expression in a string, store s[0](first number) in temp = s[0]-‘0’;

start a loop from (i=2 to l && str[i]!= ‘=’;i+=2 ) compare each character if encountered {’+’,’-’,’*’,’/’} evaluate by subtracting 48 or ‘0’ from str[i+2] and display the output. :slight_smile:

So I guess the result is that scanf was having trouble reading my input and cin wins in this case.
Thanks.
But the question is,why is commenting and uncommenting the cout line giving different outputs?
That’s weird.

u should have done scanf(" %c",&str) after scanning num1 as u can see there are spaces between each number and operator. :slight_smile:

you can read more at http://www.geeksforgeeks.org/scansets-in-c/