Exit from console

Hello, everyone,
I’m making a program in which, at the end, the user is asked to press ‘e’ to exit. My question is: Is there a function or just anything that will make the console to exit?
NOTE:
I’ve tried with exit(), system(“exit”) but it doesn’t work for me. Also I am NOT searching for something like ConsoleFree() or anything similar
Thank you in advance!
Could you post your main code?
closed account (SECMoG1T)
at the end the user is asked to press 'e' to exit
Well I guess if the program is through with excution it should just exit ,

But might be you want a conditional exit somewhere with your code, you can try somethin like

1
2
3
4
5
6
7
8
 
 char ans;
 cout <<"enter e to exit";
 cin>> ans;

  if (ans=='e'||ans=='E')
      return 0; ///or exit (0);
  
@andy1992

Just a question, do you need to #include <cstdlib> to use exit()?

EDIT: Quick search, you do.

@Nanyo

Have you included #include <cstdlib> before you tried using exit(0)?
Last edited on
You want to close the console window? Normally this is not the responsibility of the program to do so. A standard C++ program can run without a console window and instead read and write the IO to files, or elsewhere depending on the how the user wants to run it.

That said, there might be some platform dependent function that can do it.
closed account (SECMoG1T)
@Megatron if you use exit (0) you need to include #include <cstdlib> I forgot
Thank you
thanks to everyone!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
    HWND console = GetConsoleWindow();
    RECT r;
    GetWindowRect(console, &r);
    MoveWindow(console, r.left, r.top, 1200, 720, TRUE);

    system("title Mousemove v1.2");

    double timeout=0, i=0, seconds=0;
    char out;
    cout<<"How long are you going to be out?\nwrite a number between 1 and 60(1 for 1min, 60 for 60min)\n";
    cin>>timeout;
    system("cls");
    cout<<"\t\t\t STARTED\nthe program will be running for "<<timeout<<" minute/s\nyou have now 20 second to go to the window you want";
    Sleep(20000);

    while(true){
        Sleep(3000);
        system("cls");
        cout<<"\t\t\t STARTED\nthe program will be running for "<<timeout<<" minute/s\n";
        seconds=seconds+3;
        cout<<seconds<<" sec have passed";
        Sleep(3000);
        system("cls");
        cout<<"\t\t\t STARTED\nthe program will be running for "<<timeout<<" minute/s\n";
        seconds=seconds+3;
        cout<<seconds<<" sec have passed";
        Sleep(3000);
        system("cls");
        cout<<"\t\t\t STARTED\nthe program will be running for "<<timeout<<" minute/s\n";
        seconds=seconds+3;
        cout<<seconds<<" sec have passed";
        Sleep(3000);
        system("cls");
        cout<<"\t\t\t STARTED\nthe program will be running for "<<timeout<<" minute/s\n";
        seconds=seconds+3;
        cout<<seconds<<" sec have passed";
        Sleep(3000);
        system("cls");
        cout<<"\t\t\t STARTED\nthe program will be running for "<<timeout<<" minute/s\n";
        seconds=seconds+3;
        cout<<seconds<<" sec have passed CLICK";

        i++;

        MOUSEINPUT click[2] {{0, 0, 0, MOUSEEVENTF_LEFTDOWN, 0, NULL},
            {0, 0, 0, MOUSEEVENTF_LEFTUP, 0, NULL}};
        INPUT mouse_click[2] {{INPUT_MOUSE, click[0]}, {INPUT_MOUSE, click[1]}};
        SendInput(2, mouse_click, sizeof(mouse_click[0]));

        if(i==timeout*4){
            system("cls");
            cout<<"\nFINISH\n";
            cout<<"please enter 'e' to exit\n";
            cin>>out;
            if(out == 'e'){
                exit(0);                          //here is where I ask for help
            }
        }
}
return 0;
}

That is all my code :D
Last edited on
oh something else that might confuse you is that I want to close the window, not just to terminate the program or finish it.
Sorry, I wasn't very accurate when I asked!
If you run the program through the command prompt, the window will stay up when the program finished, waiting for another command.

For the window to close when finished, you will have to run the program by double clicking the .exe, I believe. It's been years since I've used Windows systems.

Topic archived. No new replies allowed.