×

# Weird thing happening with my code

 0 I'm trying to solve this easy problem from SPOJ. http://www.spoj.com/problems/ARITH2/ 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. asked 04 Dec '14, 22:48 291●2●3●19 accept rate: 10%

 0 here is your updated code :) or u can also do : store the complete expression in a string, store s0 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. :) answered 05 Dec '14, 00:52 779●2●14 accept rate: 14%
 0 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. answered 05 Dec '14, 01:40 291●2●3●19 accept rate: 10% u should have done scanf(" %c",&str) after scanning num1 as u can see there are spaces between each number and operator. :) you can read more at http://www.geeksforgeeks.org/scansets-in-c/ (05 Dec '14, 02:11)
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,911
×1,477
×671
×36
×21
×9

question asked: 04 Dec '14, 22:48

question was seen: 836 times

last updated: 05 Dec '14, 02:11