2
#include <iostream>
using namespace std;
int main () {
    int N;
    cout << " Input an integer:";
    cin >> N;
    cout << " The integer entered is:" << N << endl;        
}

when I input an Integer it returns the same value but when I input hello it gives me 1961729588.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
PommeFrite
  • 35
  • 4

4 Answers4

13

The string doesn't become an integer, the std::cin operation fails and what you get as output is the garbage value that was in N originally. Initialize N to 0, and type in "hello" you should see 0 as output.

Borgleader
  • 15,826
  • 5
  • 46
  • 62
  • 3
    Or, just to make the behavior clearer, initialize `N` to `42`, or to `123456`. – Keith Thompson Nov 06 '14 at 21:45
  • My experiments prove against your theory. Looks like the up to date compilers just reset the variable to it's default constructed value. – πάντα ῥεῖ Nov 06 '14 at 22:40
  • 2
    @πάνταῥεῖ Nope, its a change in C++11 [see my question](http://stackoverflow.com/questions/19522504/istream-behavior-change-in-c-upon-failure) about this. – Borgleader Nov 07 '14 at 00:46
5

"when I input an Integer it returns the same value but when I input hello it gives me 1961729588?."

The cin >> N; actually fails returning false for the stream state, when a input is given that cannot be converted to an integer. You can check for such error condition with

 if(!(cin >> N)) {
     cerr << "Input a valid number!" << endl;
 }
 else {
     cout << " The integer entered is:" << N << endl;
 }

The value of N will be initialized (reset) to int() (default value) which actually renders to 0.


Full live sample

#include <iostream>
using namespace std;
int main () {
    int N;
    cout << " Input an integer:";
    if(!(cin >> N)) {
        cout << "Input a valid number!" << endl;
        cout << "N = " << N << endl;
    }
    else {
        cout << " The integer entered is:" << N << endl;
    }
    return 0;
}

Input

Hello

Output

Input an integer:Input a valid number!
N = 0

This was cross checked with a Ideone code sample

I cannot reproduce getting some garbage value like 1961729588. The value was correctly reset by the std::istream& operator>>(std::istream&, int&); input operator.


Is it an issue of your current compiler's implementation, c++ standards level (-std=c++11) settings?

I have found some notes about eventual differences regarding c++ standards at cppreference.com:

enter image description here

enter image description here

Though I didn't spot what they really refer to with 'a value as described above', to be honest.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
3

When you input a non-integer the input fails. When the input fails, N retains its original value which isn't defined, i.e., writing results in undefined behavior. You should test your inputs, e.g.:

if (std::cin >> N) {
    // do something with the successful input
}
else {
    // deal with the input failure
}
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • _"N retains its original value"_ I also thought so, but have observed some different behavior. It seems to set to just `T()`, when input fails (checked on Ideone and coliru). – πάντα ῥεῖ Nov 06 '14 at 22:09
  • @πάνταῥεῖ: actually, you may be right: this could be a C++11 change. Traditionally, the value was not changed but to reflect the reason of an error could cause it to be set to some specific value. It seems, the original question wouldn't use a C++11 implementation in this case. – Dietmar Kühl Nov 06 '14 at 22:58
  • _"this could be a C++11 change"_ Yeah, looks like. Still I'm admiring you, as the absolute c++ stream expert ;-) ... – πάντα ῥεῖ Nov 06 '14 at 23:01
1

When you enter cin >> N; the compiler sees that N was declared as an int. Thus your program will call a function that will attempt to read text representing an int from cin and store the result in N.

To do this it will read as much numeric characters from cin as it can, and stop when a non-numeric character is encountered.

For example if you enter 32\n your program reads 3, then 2, then \n. When it sees the \n it stops reading, because \n is not a number. The program will push \n back on to the stream (in case we want to read it later) and store 32 in N.

Suppose instead of a number you type some word such as "hello". The your program will read h and then stop, because h is not a number. h will be pushed back onto the stream (to be read later) and nothing will be stored in N. cin will return an error since no numeric characters were read.

This still does not explain the value of 1961729588.

Notice N was never initialised. According to the C++ Standard the value of an uninitialised automatic variable is undefined. Thus the value of N will be some garbage value. In your case this was 1961729588.

Moyamo
  • 358
  • 2
  • 10