closing console window after user inputs number
Hi Folks,
Here is the code I've been trying to use in VC++ 6.0:
#include <iostream.h>
void main() {
cout << "Test" << endl;
int number;
cin >> number;
if (number == 0)
system("exit");
}
I want the console window to close automatically if the user inputs 0. So far, the application stops and "press any key to continue" appears after inputting 0. Any clues to what I'm doing wrong?
Re: closing console window after user inputs number
If you want to close the console window, just let the execution reach the end of the main method. If 0 will close the console, i take it any other number will not? Heres how I'd do it:
C++ Code:
#include <iostream>
using namespace std;
void main() {
int number;
do{
cout << "Test" << endl;
cin >> number;
}while(number != 0);
}
Furthermore, the exit function can also be used to terminate the calling process, it takes an argument that is the status code to return to the parent process. If the termination is "normal" you'd normally pass EXIT_SUCCESS.
C++ Code:
#include <iostream>
using namespace std;
void main() {
int number = system(NULL);
do{
cout << "Test" << endl;
cin >> number;
if(number == 0)
exit(EXIT_SUCCESS);
}while(true);
}
Altough I'd rarely go for the second approach.
Re: closing console window after user inputs number
in both cases above, the program DOES terminate, but the "press any key to continue" message appears, and the console window DOES NOT close. I am looking for the console window to close completely if a user inputs 0, not just for the program to stop running
Re: closing console window after user inputs number
I suspect that might be dependent of your IDE. Which one are you using?
Re: closing console window after user inputs number
I am using Visual Studio 6.0 running on WinXP. I always get the "press any key to continue" message and the console window remains open.
Re: closing console window after user inputs number
I only get that when choosing "Start without debugging". Try building a release version and execute it outside of the IDE.