Not looping after wrong input

Im currently attempting to create a text based game but early on im encountering a problem where if you were to type 3 instead of 1 or 2 it would end the program instead of looping back to ask for 1 or 2 again. i'm also having trouble implementing a save and load feature as well if anyone can to help me with this.

#include <iostream>
#include <string>

using namespace std;

int main()
{
int option;
int Title;

cout << "Hemlok\n";
cout << "MENU\n\n";

cout << "1 To start new game\n";
cout << "2 To load your game\n";
cout << "Insert option now: ";
cin >> option;

if(option==1)
{
cout << "Welcome to Hemlok, the mighty kingdom of Orcs and Humans.\n";
cout << "Pick your race: ";
cout << "1 - Human\n";
cout << "2 - Orc\n";
int playerRace;
cin >> playerRace;

switch (playerRace)
{
case 1:
cout << "You picked the Human race.\n";
{
{
cout << "Enter your heros name:";
string name;
cin >>name;
cout << "Your journey begins, " << name <<".\n";

cout << "///////////////////////CHAPTER 1 : OH MY CAININES/////////////////////////\n";

}

int save;

cout << "You must choose one.\n";
cout << "1 - \n";
cout << "2 - \n";
cout << "Choose now: ";

cin >> save;
switch (save)
{

case 1:
cout << "You have decided case 1.";
{
}
break;

case 2:
cout << "You have decided case 2.";
break;

default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
}

}
break;

case 2:
cout << "you picked the Orc race.\n";
{
cout << "Enter your heros name:";
string name;
cin >>name;
cout << "Your journey begins, " << name <<".\n";
}
break;

default:
cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
}
}
if(option==2)
{

}

return 0;
}
Last edited on
closed account (j3Rz8vqX)
Use the
[code][/code]
tags.

Or the <> "source code" tag available in the format editor of this webpage, when you post your code.

Else some folks may not bother to look at your code.

The below is your code with the appropriate indentation and is posted as code:
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int option;
    int Title;

    cout << "Hemlok\n";
    cout << "MENU\n\n";

    cout << "1 To start new game\n";
    cout << "2 To load your game\n";
    cout << "Insert option now: ";
    cin >> option;

    if(option==1)
    {
        cout << "Welcome to Hemlok, the mighty kingdom of Orcs and Humans.\n";
        cout << "Pick your race: ";
        cout << "1 - Human\n";
        cout << "2 - Orc\n";
        int playerRace;
        cin >> playerRace;

        switch (playerRace)
        {
            case 1:
                cout << "You picked the Human race.\n";
                {
                    {
                        cout << "Enter your heros name:";
                        string name;
                        cin >>name;
                        cout << "Your journey begins, " << name <<".\n";
                        cout << "///////////////////////CHAPTER 1 : OH MY CAININES/////////////////////////\n";
                    }
                    int save;
                    cout << "You must choose one.\n";
                    cout << "1 - \n";
                    cout << "2 - \n";
                    cout << "Choose now: ";

                    cin >> save;
                    switch (save)
                    {
                        case 1:
                        cout << "You have decided case 1.";
                        {
                        }
                        break;

                        case 2:
                        cout << "You have decided case 2.";
                        break;

                        default:
                        cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
                    }
                }
                break;
            case 2:
                cout << "you picked the Orc race.\n";
                {
                    cout << "Enter your heros name:";
                    string name;
                    cin >>name;
                    cout << "Your journey begins, " << name <<".\n";
                }
                break;

                default:
                cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
        }
    }
    if(option==2)
    {

    }
    return 0;
}


You can repeat your program by using a loop of your choice:
Example of do-while loop:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int option;
    int Title;

    do
    {
        cout << "Hemlok\n";
        cout << "MENU\n\n";

        cout << "1 To start new game\n";
        cout << "2 To load your game\n";
        cout << "Insert option now: ";
        cin >> option;

        if(option==1)
        {
            //Does something...
        }
        if(option==2)
        {
            //May do something
        }
        //You need to implement an exit code!
    }while(option<=2 || option>=1);
    return 0;
}


Make sure to implement an exit procedure, example:
1
2
3
4
        if(option==3)
        {
            break;//Break out of the current loop!
        }

Or:
1
2
3
4
5
        if(option==3)
        {
            //Prompt any info, because loop will certainly break!
        }
    }while(option<=2 || option>=1 && option!=3);


Tip: Handing you the fish won't help you, the below is long but will teach you the flow control operations:
http://www.cplusplus.com/doc/tutorial/control/

Use your browsers find feature (usually ctrl-f) and seek "The do-while loop".
Last edited on
Thank you!, this has saved many future complaints in my game.
Topic archived. No new replies allowed.