My First C++ Program...

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
I just want to know how I can set it so that it doesn't close the DOS window when I compile/execute it.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Option 1:
Open a DOS window and run it from there, instead of double-clicking the exe.

Option 2:
add:
system("pause");
as the last line of main() before returning.

 

screw3d

Diamond Member
Nov 6, 2001
6,906
1
76
I think system("pause") only works on Windows.. so you might wanna take note in case you run it elsewhere.

Something irrelevant here.. but you can use using namespace std; instead of putting std:: on every line.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
I actually just noticed cin.get() didn't work on the second call. No idea why...
What's the C++ equivalent of getch()? Anyone?
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Originally posted by: xtknight
I actually just noticed cin.get() didn't work on the second call. No idea why...
What's the C++ equivalent of getch()? Anyone?

cin

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Why does cin.get() not work on the second call? It wasn't halting the program for me as I remember, and apparently not for the OP either.
 

VIAN

Diamond Member
Aug 22, 2003
6,575
1
0
you could always loop it.

At the end, you could have:

while(1);

So that it loops forever.

or turn the whole thing into a loop and set up a condition at the end.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
try doing a void main() and do not return 0;.
That won't help.
What's the C++ equivalent of getch()? Anyone?
I usually just use getchar() anyway.

If you're using Visual Studio, there should be a "!" (exclamation point) button in your toolbar somewhere that will show you the output after the window has closed.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: xtknight
Why does cin.get() not work on the second call? It wasn't halting the program for me as I remember, and apparently not for the OP either.

cin.get() does not clear the input buffer of carriage returns, so the next retrieval completes immediately. you can work around it by using the ignore methd cin.ignore('\n', 999); or something like that
 

vtqanh

Diamond Member
Jan 4, 2001
3,100
0
76
Originally posted by: dighn
Originally posted by: xtknight
Why does cin.get() not work on the second call? It wasn't halting the program for me as I remember, and apparently not for the OP either.

cin.get() does not clear the input buffer of carriage returns, so the next retrieval completes immediately. you can work around it by using the ignore methd cin.ignore('\n', 999); or something like that

or you can just flush the input buffer and then do cin.get()