0

I've made some C++ dos programs, and it runs fine, and does everything I want it to, but the moment it does the last line of code, it exits. I use Visual C++, and it stays on when I preview it in there, but after I compile it, and run it in the directory it was stored in, it exits right away after it's done.

Is there anyway to solve this? I've tried "System: Pause", but I think i'm doing it wrong.

5
Contributors
8
Replies
10
Views
14 Years
Discussion Span
Last Post by Dani
0

Try adding the following line just before you return from main():

cin.get();

for example, a hello world program:

#include <iostream>
using namespace std;

int main()
{
cout << "Hello, world" << endl;
cin.get();
return 0;
}

Edited by Dani: Formatting fixed

0

If the "cin" doesn't work for you - try

system("PAUSE");
return 0;
}

This may not help whatsoever - I use Dev C++ and this is how it is done.

Hope it helps.....

The MuddBuddha ->

0

"cin" is standard C++. If you have a C++ compiler it will work.

system("pause");

is a platform specific call. It's generally preferable to use portable solutions rather than platform specific where such a portable solution exists.

0

Err...I'm a beginner in this. I'm not sure if adding a getchar() before your program ends would help or not. ;)

0

Mr Gates, were any of these suggestions helpful? I take it you got it working? A simple user input at the end of the program should offset it automatically exiting.

0

Yes, they seem to work great. I'm sorry I didn't reply earlier. :x

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.