Reputation: 35
#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
.
Upvotes: 2
Views: 597
Reputation: 1
"when I input an Integer it returns the same value but when I input
hello
it gives me1961729588?
."
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
.
#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:
Though I didn't spot what they really refer to with 'a value as described above', to be honest.
Upvotes: 5
Reputation: 368
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.
Upvotes: 1
Reputation: 153792
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
}
Upvotes: 3
Reputation: 15916
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.
Upvotes: 13