Console keeps closing

Jul 28, 2009 at 4:26pm
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;
}
Jul 28, 2009 at 5:00pm
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
Jul 28, 2009 at 5:02pm
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.
Jul 28, 2009 at 5:25pm
What do you mean in the same directory? Sorry. i am way confused right now.
Jul 28, 2009 at 5:37pm
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 Jul 28, 2009 at 5:37pm
Jul 28, 2009 at 7:31pm
Just listen to Bazzy. You're problem isn't with your include it's with your use of cin >>
Aug 20, 2009 at 8:22pm
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!!

Aug 20, 2009 at 10:27pm
thats not right... its not working on my machine ;)...
Aug 21, 2009 at 1:17pm
try
1
2
3
#include <stdio.h>
...
system( "pause" );
Aug 22, 2009 at 12:48am
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!
Aug 22, 2009 at 3:26am
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;
}


Aug 22, 2009 at 8:42am
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.
Aug 22, 2009 at 8:43am
closed account (z05DSL3A)
Have you tried the keep_window_open() function from std_lib_facilities.h?
Aug 23, 2009 at 5:32am

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.