According to my textbook, "C++ Primer Plus, Sixth Edition", if I input a character into an int
variable, it is wrong behavior. So the variable doesn't change and the wrong input is stored in the buffer. Also, the cin
object sets an error flag, and the cin
method return false.
But, when I write code like the following and input a character into an int
variable n
, it becomes 0.
As I learned, it must be the first value which is 9.
Why does it become 0?
#include<iostream>
using namespace std;
int main()
{
int n=9;
cout << "now the number n is = " << n << endl;
cout << "enter the new number : ";
cin >> n;
cout << "new number n is = " << n << endl;
return 0;
}