I ran the following code twice.
Once when I input Carlos 22
, the program ran correctly and keep_window_open()
apparently worked as the console window stayed open, and displayed the "Please enter a character to exit"
message.
But when I input 22 Carlos
the program works as expected but the keep_window_open()
does not work - it simply closes as if I did not have that line in there at all.
What is the reason for this behavior? And what is the best way I can keep the window open in all circumstances?
My code:
#include "std_lib_facilities.h"
// read name and age
int main()
{
cout << "please enter your name and your age\n";
string first_name = "???";
int age = -1;
cin >> first_name >> age;
cout << "Hello, " << first_name << " (age " << age << ")\n";
keep_window_open();
}
The code and exercise are from Programming Principles and Practice Using C++.