You are not logged in. Please login at to post your questions!


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.

asked 04 Dec '14, 22:48

h1ashdr%40gon's gravatar image

accept rate: 10%

edited 05 Dec '14, 00:10

here is your updated code :)


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

rjohari23's gravatar image

accept rate: 14%

edited 05 Dec '14, 01:13

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

h1ashdr%40gon's gravatar image

accept rate: 10%

edited 05 Dec '14, 01:40

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

you can

(05 Dec '14, 02:11) rjohari233★
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • 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:


question asked: 04 Dec '14, 22:48

question was seen: 837 times

last updated: 05 Dec '14, 02:11