1

I have the below code:

int a , b , sum;
cin>>a>>b;
sum=a+b;
cout<<sum;

I am completely aware that you cannot store floating point values in an integer. So during the first run of my program:

10 2.5

12

I get the expected output of 12 as the decimal part of 2.5 is ignored

In the second run I put the floating point value first:

2.5 10

442837

I get a garbage value , anyone knows what going on?

Help is appreciated :)

10
  • "the compiler would ignores the decimal part of 2.5" - It's not the compiler which ignores it, as you input this number during runtime. Commented Feb 12, 2015 at 14:25
  • @barakmanos yup my bad
    – m0bi5
    Commented Feb 12, 2015 at 14:26
  • It would be better if you'd read everything into doubles and then convert those to ints if needed. In any case: Check the status of your streams: if (!cin) { error(); }.
    – 5gon12eder
    Commented Feb 12, 2015 at 14:27
  • 1
    And that may help you to understand the rest of the problem (hint: it's the cin object that "ignores" the decimal part of 2.5 during runtime). Commented Feb 12, 2015 at 14:27
  • 5gon12eder is right, i bet your cin needs to be cleared
    – iedoc
    Commented Feb 12, 2015 at 14:27

2 Answers 2

13

Initialize your variables and you will see what is happening. It isn't ignore the decimal. It is causing an error that stops the parsing. So the crazy number you see is actually the value of the uninitialized integer.

Here is what is happening: When you type "10 2.5" it puts 10 into a, and 2 into b. It does not ignore th e 0.5. To understand what actually happens, try this code:

int a=100 , b=200 , c=300, sum;
cin>>a>>b>>c;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;

Then enter in "10 2.5" and a will be 10, b will be 2, and c will be 300! The ".5" caused cin to get an error, and so it just left c at the default value. But since you only read 2 values, it seemed to work just fine. So try that version with your second set of inputs "2.5 10". A will be 2, then b will be 200 and c will be 300. That shows how cin encountered an error when it saw the decimal point, and just gave up.

And finally for fun, remove the initializations in my example, and watch how you get crazy values for b and c.

4
  • Hah! Perfect answer , but is it always like this in SO(its my first question); people get downvotes for asking a question?
    – m0bi5
    Commented Feb 12, 2015 at 14:35
  • Yeah, it is adding the uninitialized values. Another thing to do to see this is to add this line: "cout << "good: " << cin.good() << endl;" before and after the cin. The function good() returns true if the cin stream is in a happy state, and false if it is not. You will find that typing in the .5 will change cin to being not good.
    – Moby Disk
    Commented Feb 12, 2015 at 14:37
  • thx for the additional good() , never knew about that function
    – m0bi5
    Commented Feb 12, 2015 at 14:39
  • Looks like @5gon12eder had a shorter way by using if (!cin)
    – Moby Disk
    Commented Feb 12, 2015 at 14:40
1

Quote from std::istream::operator>>:"Extracts and parses characters sequentially from the stream to interpret them as the representation of a value of the proper type, which is stored as the value of val."

Check the std::istream::operator>> for an in depth look at how reading the input works.

Moreover, you could std::cout << std::cin.rdstate(); after reading a double value into an int, to see that the cin object gets into an error state at such an operation. The answer I think, is that cin >> operation does not do implicit type conversions, and is thrown into an error state.

1
  • Thanks for sharing knowledge :) +1
    – m0bi5
    Commented Feb 12, 2015 at 14:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.