Weird thing happening with my code

c
c-plus-plus
comment
cout
problem
weird

#1

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


Here is my code : (My logic might be kind of weird)
http://ideone.com/upiUY8
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.


#2

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+=2 ) compare each character if encountered {’+’,’-’,’*’,’/’} evaluate by subtracting 48 or ‘0’ from str[i+2] and display the output. :slight_smile:


#3

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.


#4

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/