Wanna ask some codes Please Help!!!!

I wrote this codes and i wanna ask how come if i enter letters not numbers and it will stuck!!
Also how can i add an error code on the numbers and letters thats if is not 1,2,3,4?

#include <iostream>
#include <cstdlib>
using namespace std;
void menu();
void mainMenu();
void optionsMenu();
void options();
int choice1 = 0;
int choice2 = 3;
int main(int argc, char** argv)
{
menu();
return 0;
}
void menu()
{ do
{
choice2 = 0;
mainMenu();
switch(choice1)
{
case 1:
cout << ">> PLAY Menu Item Selected!\n";
cout << ">> Playing game...\n";
cout << ">> Continuing with program\n";
break;
case 2:
options();
break;
case 3:
break;
}
} while(choice1 != 3);
}
void options(void)
{ do
{
optionsMenu();
switch(choice2)
{
case 1:
cout << "So difficult!\n";
break;
case 2:
cout << "Beep!\n";
break;
case 3:
break;
default: break;
}
}
while(choice2 != 3);
}
void mainMenu(void)
{
cout << "Please select from one of the following options:\n";
cout << "1: Play\n";
cout << "2: Options\n";
cout << "3: Quit\n";
cout << "4: Help\n";
cout << "Enter (1,2,3 or 4 only): ";
cin >> choice1;
}
void optionsMenu(void)
{
cout << "Options Menu\n";
cout << "1 - Difficulty\n";
cout << "2 - Sound";
cout << "3 - Back\n";
cout << "Please choose: ";
cin >> choice2; }
if i enter letters not numbers

If the program is expecting an integer (a number) but the user enters a letter, then the cin.fail() flag will be set. In order to fix that, you would need to both clear the error flags and empty the input buffer.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int choice;
if (!(cin >> choice))
{
    cin.clear();            // reset the error flags
    cin.ignore(1000, '\n'); // remove up to 1000 characters 
                            // or until newline is found, from input buffer.
}
else
{
    switch (choice)
    {
        case 1:
            // etc.
    }
}


But, you can avoid the need to worry about that if the input is read as a character instead of an integer.

1
2
3
4
5
6
7
char choice;
cin >> choice;
switch (choice)
{
    case '1':
       // etc
}
So is that the way of enter numbers not letters?
But for example it is 1,2,3,4. So if i enter 9.
Should be error.
Also letters too.
i still dont get it mate?
You could use either of the methods suggested. Use the switch/case to take appropriate action.
int
takes only integer values(0-9)

char

takes character values meaning a-z, 0-9, and symbols.

choosing which one to use is a matter of taste, and how to test if you want numbers only you can always use other functions like isDigit or isAlpha and throw and error if not the one you want.

The program freezes because it does not recognize the input and does not know what to do there on. unless you tell it to ignore what was input like Chervil did at the top, then it will continue as normal.
i still have no idea can you please rewrote it for me?! PLEASE!!!
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
84
85
86
87
88
89
90
91
# include <iostream>
# include <cstdlib>
using namespace std;

void menu();
void mainMenu();
void optionsMenu();
void options();

int choice1 = 0;
int choice2 = 3;

int main(int argc, char** argv)
{
    menu();
    return 0;
}

void menu()
{
    do
    {
        choice2 = 0;
        mainMenu();

        switch(choice1)
        {
            case 1:
            cout << ">> PLAY Menu Item Selected!\n";
            cout << ">> Playing game...\n";
            cout << ">> Continuing with program\n";
            break;
            case 2:
            options();
            break;
            case 3:
            break;
        }
    } while(choice1 != 3);
}

void options(void)
{
    do
    {
        optionsMenu();
        switch(choice2)
        {
            case 1:
            cout << "So difficult!\n";
            break;
            case 2:
            cout << "Beep!\n";
            break;
            case 3:
            break;
            default: break;
        }
    }
    while(choice2 != 3);
}

void mainMenu(void)
{
    cout << "Please select from one of the following options:\n";
    cout << "1: Play\n";
    cout << "2: Options\n";
    cout << "3: Quit\n";
    cout << "4: Help\n";
    cout << "Enter (1,2,3 or 4 only): ";
    if (!(cin >> choice1))
    {
        cin.clear();            // reset the error flags
        cin.ignore(1000, '\n'); // remove up to 1000 characters
                                // or until newline is found, from input buffer.
    }
}

void optionsMenu(void)
{
    cout << "Options Menu\n";
    cout << "1 - Difficulty\n";
    cout << "2 - Sound";
    cout << "3 - Back\n";
    cout << "Please choose: ";
    if (!(cin >> choice2))
    {
        cin.clear();
        cin.ignore(1000, '\n');
    }
}
Hey i rewroted the whole thing myself but it didnt runs properly what did i do wrong?!

#include <iostream>
#include <cstdlib>

using namespace std;

void menu();
void mainMenu();
void optionsMenu();
void options();
int choice1 = 0;
int choice2 = 3;
int number;

int main(int argc, char** argv)
{

cout << "WELCOME TO THE SIMPLE TEXT GAME!";

menu();

}

void menu()
{

do {
choice1 = 0;
mainMenu();


switch(choice1)
{


case 1:
cout << ">> PLAY Menu Item Selected!\n";
cout << ">> Continuing with program\n";
break;

case 2:
cout << ">> Option Menu Item Selected!\n";
cout << ">> Continuing with program\n";
break;

case 3:
cout << ">> Quit Menu Item Selected!\n";
cout << "Goodbye!\n";
break;

case 4:
cout << ">> Help Item Selected!\n";
cout << ">> Continuing with program\n";
break;

cin >> number;
while (number > 4 );
{
cout << "ERROR: Invalid option selected! Please select from 1,2,3 or 4\n";
cin >> number;
}



}
} while (choice1 != 3);

}

void mainMenu(void)
{
cout << "Please select from one of the following options:\n";
cout << "1: Play\n";
cout << "2: Options\n";
cout << "3: Quit\n";
cout << "4: Help\n";
cout << "Enter (1,2,3 or 4 only): ";
cin >> choice1;
}

void optionsMenu(void)
{
cout << "Please select from one of the following options:\n";
cout << "1: Play\n";
cout << "2: Options\n";
cout << "3: Quit\n";
cout << "4: Help\n";
cout << "Enter (1,2,3 or 4 only): ";
cin >> choice2;
}
Copy the code above and It will works
Topic archived. No new replies allowed.