Hello, I am a total beginner at c++

I am writing a console program and I am wondering how I should do to exit the
program, like if the user is asked

Do you want to continue ( yes/no) and the user enters no then I want the console
to shut down...

Within main, finish the program with return 0;

std::string isShutdown;

std::cout << "Shutdown? Y/N" << std::endl;
std::cin >> isShutdown;

if (isShutdown == "Y")
{ return 0;}

You can also end a program by using abort() and exit(), but they place additional responsibilities on the conscientious programmer. Using return is the standard way.

Edited 5 Years Ago by Moschops: n/a

do I need to include something to use exit()? The code you wrote wont shut the console down ...It makes the console write ...enter any key to continue..and the shut down

The console is not part of your program. If you have a console, and you start your program within that console, when your program finishes, that console will still be there.

It sounds to me like you're not starting the console, though; I expect your IDE is starting it for you, and then closing it for you afterwards. Are you running your program by typing its name into a command line, or by pushing a button on an IDE?

As I recall, what you describe is symptomatic of running a console program from within Visual Studio in debugging mode (SHIFT+F5 rings a bell). it's something your IDE does for you. If you don't want it, don't run in debug mode.

Edited 5 Years Ago by Moschops: n/a

Use a if statement and a state variable.

int main(){
 bool shouldContinue = true;
 do{
    /***code here***/

   //ask user if he want to continue
   char input;
   cout << "Continue(y/n) : " ;
   cin >> input;
   if(input != 'y') shouldContinue = false;
 }while(shouldContinue);


}

Edited 5 Years Ago by firstPerson: n/a

Will that actually solve the "problem" here? That a console does not automatically close itself when a program running within it finishes?

Edited 5 Years Ago by Moschops: n/a

Will that actually solve the "problem" here? That a console does not automatically close itself when a program running within it finishes?

If he is using an IDE like visual studio then the IDE automatically pauses the console before an input, so in a sense it wouldn't automatically shut it down, but when he runs the .exe file without an IDE, the console doesn't get paused, so the above would close it.
But I think, I may have interpreted his question incorrectly. I was proposing a proper way to flow the program to exit rather than simply calling exit. Judging by the nature of his post and wording, I assumed he was looking something more towards what I posted, but who knows.

if you are using windows then can you use

system("exit");

.
The content inside the bracket of system() is your command prompt instruction.

if you are using windows then can you use

system("exit");

.
The content inside the bracket of system() is your command prompt instruction.

That's absolutely terrible. What does system("exit"); do better than return 0; . Give you one guess what it does worse...

erm.. is not applicable on other os right? my program also have the return 0 at the end. but in some condition it will terminate the prompt.
mayb the problem is when you still have to work on next program, so with return 0 it will end current program and start the next? but with system("exit") it will totally close it?

mayb the problem is when you still have to work on next program, so with return 0 it will end current program and start the next? but with system("exit") it will totally close it?

That's an impressive feat of misplaced logic. No, you were correct in saying that system("exit") is not portable. C++ provides a portable exit function already:

#include <cstdlib>
#include <iostream>

void foo()
{
    std::cout<<"Exiting from foo\n";
    std::exit(0);
}

int main()
{
    foo();
    std::cout<<"Returning from main\n";
    return 0;
}

There's no need to do stupid stuff like system("exit") .

Edited 5 Years Ago by Narue: n/a

Presumably, exit in a windows shell, run via system("exit"); will cause the shell to close (granted, mid-program, which is akin to turning off your PC by deliberately crashing the OS).

Ultimately, this is not a C++ question at all; it's an OS question about making things happen when you've finished running a program.

okey, thank for the correction.
So system("exit") is replace with exit() with lib <cstdlib> or <stdlib.h>
Actually i use system("cls") and system("pause") also. so what should i use to do these?

Lots of options. For example,


system("cls"); becomes

for (int i=0;i<100;++i)
{
std::cout << std::endl;
}


system("pause"); becomes

std::cin.get();

This article has been dead for over six months. Start a new discussion instead.