Is there any exit commands?

I tried to make a program for login, but I really want to make an exit command.
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
#include <iostream>

using namespace std;

int main()
{
    string user;
    string password;

    cout << "Hello: ";
    getline(cin, user);

    if(user=="User1")
    {
        cout << "Password: ";
        getline(cin,password);
        if(password=="122345")
        {
            cout << "Login succes!";
        }
        else
        {
            cout << "Wrong password!";
        }
    }
    else if(user=="User2")
    {
        cout << "Password: ";
        getline(cin, password);
        if(password=="122345")
        {
            cout << "Logare reusita!";
        }
        else
        {
            cout << "Logare nereusita!";
        }
    }
    else
    {
        cout << "Acest User nu exista!";
    }
    return 0;
}
Last edited on
Not clear what you mean by "exit command".
Do you mean to ask the user if he/she wants to exit the login program?
Or do you mean to exit your program, if for example the user enters the wrong password?

If you mean the later, you have two choices.
1. Since your program is all within main(), you can simply return at any point you wish.
2. You can call exit()
http://www.cplusplus.com/reference/cstdlib/exit/?kw=exit

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
exit() and terminate() depending on your intent and needs. These are abused a lot and were really meant to be used for critical fails. I prefer to find a way to exit the program normally, via a return in main, using some logical path from the failure back out. That isn't always possible, but try to cut that path first.
Last edited on
Topic archived. No new replies allowed.