1

I have MISTAKENLY found the scenario while executing the following block of code

#include <iostream>
using namespace std;
int main()
{
    int input1;
    float input2;

    cout << "Enter a real number :";
    cin >> input1;
    cout << "The int number is " << input1 << endl;

    cout << "Enter another number :";
    cin >> input2;
    cout << "The float number is " << input2 << endl;
}

The output for the above is

Enter a real number :a
The int number is -858993460
Enter another number :a
The float number is -1.07374e+08

Can anyone kindly explain how internally the above scenario is getting handled resulting in the above scenario ?

Note -

  • Running the above in VS2015.

As i am newly experimenting with C++, please point me to any reference if i have missed in the process.

mpiatek
  • 1,313
  • 15
  • 16
sasuke
  • 375
  • 1
  • 6
  • 12

2 Answers2

4
int input1;
float input2;

At this point, both input1 and input2 have undefined values since you didn't initialize them.

std::cin was expecting an integer to be entered but you entered 'a', which made std::cin to fail. That failure persists such that no extraction operation can be performed with std::cin until the failbit is cleared.

After your failed input operations, input1 and input2 are still "undefined". Printing them lead to Undefined Behavior.

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • Yes you are right, indeed the correct behaviour, i initialized them with 0 and now the output seems to be correct even if i give a wrong input. Thanks for the explanation. I will accept this as the answer. – sasuke Mar 04 '17 at 18:40
  • Use `if(!cin)cout<<"failed";` after `cin>>input` to verify that `cin` actually failed. – Gaurav Sehgal Mar 04 '17 at 18:42
  • @GauravSehgal thanks for the input... it worked as you said. – sasuke Mar 04 '17 at 18:44
  • 1
    @sasuke, see [this](http://stackoverflow.com/questions/17928865/correct-way-to-use-cin-fail), and [this](http://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input), and the [states](http://en.cppreference.com/w/cpp/io/ios_base/iostate) that `std::cin` can assume – WhiZTiM Mar 04 '17 at 18:45
  • or even `if (cin >> input1)` – mpiatek Mar 04 '17 at 18:45
  • @WhiZTiM thanks for your responses. Got to know something new even by making such silly mistake. – sasuke Mar 04 '17 at 18:49
  • @mpiatek , yes your suggestion is pretty quick one. Thanks . – sasuke Mar 04 '17 at 18:51
  • Note that C++11 changed this to store 0 on failure, at least for `int`. I assume it's the same for floating-point overloads. – chris Mar 04 '17 at 18:59
2

The extraction operator does not change the variables when the stream cannot be interpreted as a valid value of the appropriate type. Therefore you see uninitialized values for input1 and input2. You can check the failbit on cin to see whether the extraction operator was successful.

For example:

    int input1;
    cout << "Enter a real number :";
    cin >> input1;
    if(cin.good())
    {
        cout << "The int number is " << input1 << endl;
    }
    else
    {
       cout << "The input was not a number." << endl;

       // skip to the end of the input
       cin.clear();
       cin.ignore(INT_MAX, '\n');
    }
joshuanapoli
  • 2,509
  • 3
  • 25
  • 34