|
-
Sep 22nd, 2008, 10:18 AM
#1
Thread Starter
Lively Member
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?
-
Sep 22nd, 2008, 10:53 AM
#2
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.
Last edited by Atheist; Sep 22nd, 2008 at 10:59 AM.
-
Sep 22nd, 2008, 01:07 PM
#3
Thread Starter
Lively Member
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
-
Sep 22nd, 2008, 02:45 PM
#4
Re: closing console window after user inputs number
I suspect that might be dependent of your IDE. Which one are you using?
-
Sep 23rd, 2008, 07:31 AM
#5
Thread Starter
Lively Member
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.
-
Sep 23rd, 2008, 07:35 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|