Console keeps closing

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"
using namespace 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;
}
Don't use >> with cin, use getline: http://www.cplusplus.com/forum/articles/6046/
cin >> doesn't check the input type and leaves the last '\n' character on the stream
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.
What do you mean in the same directory? Sorry. i am way confused right now.
He means put "std_lib_facilities.h" in the same folder as your .cpp file. Then all you have to do is...

#include "std_lib_facilities.h"
Last edited on
Just listen to Bazzy. You're problem isn't with your include it's with your use of cin >>
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!!

thats not right... its not working on my machine ;)...
try
1
2
3
#include <stdio.h>
...
system( "pause" );
Sigh, the unending topic, with the unending bad advice.
http://www.cplusplus.com/forum/articles/7312/
http://www.cplusplus.com/forum/beginner/1988/

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.

Good luck!
I'm not sure about this method. This is what I do for Windows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#ifdef WIN32
#include <windows.h>
#endif
using namespace std;

int main()
{
cout << "Hello World!";
int seconds = 1;
        #ifdef WIN32
           Sleep(seconds*3000);
        #else
           sleep(seconds);
        #endif
return 0;
}


I do this:

1
2
3
4
5
6
7
8
9
#include <iostream>

//...

int main() {
    someCode;
    std::cin.get();
    return 0;
}


Which waits for a newline to be read, and then continues execution.
closed account (z05DSL3A)
Have you tried the keep_window_open() function from std_lib_facilities.h?

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"
using namespace 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";
	system("pause");
	return 0;
}	
Topic archived. No new replies allowed.