I am completely new to C++ and am trying to write an extremely basic program, but I am having issues with initializing an integer. I have stripped it down to a very small program that still has the issue:
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter your age\n";
int age = -1;
cin >> age;
cout <<"\n\n Your age is " << age << "\n\n";
}
I read that if I attempt to input a string, e.g. abc
to the age
variable, then the input should fail and the value should be left alone and therefore it should print Your age is -1
.
However, when I run this program and type abc
, then it prints Your age is 0
. Why?