Ive tried multiple things to keep the console from closing but after i type in name and age it still closes before i get to see the output. ive tried cin.get() and cin.ignore(). cant figure it out. any ideas? im using visual c++ 2008 express edition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "C:\Users\Tyler\Documents\Visual Studio 2008\Projects\std_lib_facilities.h"
usingnamespace std;
int main()
{
cout << "Please enter your first name and age:\n";
string first_name;
int age;
cin >> first_name;
cin >> age;
cout << "Hello, " << first_name << "(age " << age << " )\n";
cin.get();
return 0;
}
I don't think your include path will work. Why not put the header file in the same place as the source file and simply use #include "std_lib_facilities.h" ?
You said you'd already tried cin.ignore(). Try changing the include path so that the source and header file are in the same directory and then run the program.
Surprisingly, the fix for this problem is very simple. I found that when I ran my programs, I always hit the “Play” button on the Debug toolbar. That green arrow actually means “Start Debugging.”
If you press Ctrl+F5, or, in the “Debug” menu click “Start Without Debugging” when you want to run your program, the console window will not automatically close after your program is finished. You will get the “Press Any Key To Continue…” message. Voila!!
Always read all user input as a complete line of text (including the newline AKA Enter key). Then use cin.ignore( ... ); as illustrated in the above links.