Hi everyone,
I began to learn C++ from Stroustrup book, and I already have a little problem with one of the first programs:
int main()
{
cout << "Please enter your first name and age:\n";
string first_name = "???"; // variable pour une chaine
int age = -1; // variable pour un entier
cin >> first_name >> age;
cout << "Hello, " << first_name << " (age " << age <<")!\n";
return 0;
}
Here, age should keep its initial value (-1) if the user put a wrong type, like 22 Carlos (first_name become "22", but Carlos isn't an integer).
My problem is that if I type 22 Carlos, first_name get 22 but age become 0 instead of -1... Any idea? I use Code::Blocks on Linux.
Maybe the compiler you were using with Code::Blocks sets the int variable to zero when you attempt to assign invalid data to it, while in VS the compiler keeps the original value.