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

×

Weird thing happening with my code

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

h1ashdr%40gon's gravatar image

2★h1ashdr@gon
2912319
accept rate: 10%

edited 05 Dec '14, 00:10


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. :)

link

answered 05 Dec '14, 00:52

rjohari23's gravatar image

3★rjohari23
779214
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.

link

answered 05 Dec '14, 01:40

h1ashdr%40gon's gravatar image

2★h1ashdr@gon
2912319
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 read more at http://www.geeksforgeeks.org/scansets-in-c/

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

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

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

question asked: 04 Dec '14, 22:48

question was seen: 837 times

last updated: 05 Dec '14, 02:11