while loops in switches?

So my code is pretty basic, just simply choosing a difficulty level in a game, however even that I can't even manage to get right, my problem is occurring with the last few lines of code, I can't seem to make it loop properly if the char entered isnt "acceptable", does anyone have any idea what i'm doing wrong?
*edit*
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
 #include <iostream>

using namespace std;
char Difficulty;
char Acceptable = ('E' || 'e' || '1' || 'M' || 'm' || '2' || 'H' || 'h' || '3' || 'I' || 'i' || '4');
void PickDifficulty()
{
    switch (Difficulty)
    {
        case 'E':
        case 'e':
        case '1':
            cout << "You have selected: \neasy\nIf you dont win, you're seriously dumb" << endl;
            break;
        case 'M':
        case 'm':
        case '2':
            cout << "You have selected: \nMedium\nYou should win" << endl;
            break;
        case 'H':
        case 'h':
        case '3':
            cout << "You have selected: \nHard\nGood luck" << endl;
            break;
        case 'I':
        case 'i':
        case '4':
            cout << "You have selected: \nImposssible\nAre you dumb?"<< endl;
        default:
            {
            cout << "Don't try and trick me" << endl;
            cout << "Pick again" <<endl;
            cin >> Difficulty;
            }
}
}

int main()
{
    cout << "Welcome to guess my number!" << endl;
    cout << "The game is pretty simple, the number is between 1-100" << endl;
    cout << "Please select what difficulty you would like to use" << endl;
    cout << "Easy - 1" << endl;
    cout << "Medium - 2" << endl;
    cout << "Hard - 3" << endl;
    cout << "Impossible - 4" << endl;
    cin >> Difficulty;
    PickDifficulty();
    while (Difficulty != Acceptable);
    {
        PickDifficulty();
    }

    return 0;
}
Last edited on
1
2
3
char Acceptable = ('E' || 'e' || '1' || 'M' || 'm' || '2' || 'H' || 'h' || '3' || 'I' || 'i' || '4');
//is equivalent to
char Acceptable = '☺'; 

A single char can contain only one character.

You can return bool from PickDifficulty, indicating if difficulty was picked correctly.
I knew char could only equal 1 letter, but I thought you could use those || operators.
tbh if that isn't the case, I have no idea how to code it, could you possibly give me an example or change the above to an acceptable format?
1
2
3
4
5
6
7
8
char difficulty;
const std::string acceptable = "e1m2h3i4";
do {
    std::cin >> difficulty;
    difficulty = tolower(difficulty);
} while(acceptable.find(difficulty) == std::string::npos) 
PickDifficulty(difficulty);
//Loop while entered character is not acceptable one. 
I am most likely missing something here, but surely the string "e1m2h3i4" would only be valid if the user entered that?
The rest of the code I'm afraid I don't understand, sorry, this must be frustrating for you but i really dont get it.
How can i make it so that e,1,m,2,h,3,i,4 are all considered acceptable entries and anything else not, without typing them out 1 by 1?
Why don't you just run the input through your switch statement? If it gets to the default clause, then you know the user entered an invalid value.
Yes but then I can't get it to loop from there
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int difficulty = 0;
while (!difficulty)
{
  char c;
  cout << "Chose! ";
  cin >> c;
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );

  switch (toupper( c ))
  {
    case 'E':
    case '1':
      difficulty = 1; break;

    ...

    default:
      cout << "Try again: ";
  }
}
cout << "Yay! You chose difficulty level " << difficulty << ".\n";

Hope this helps.
yes I understand that, but that loop will repeat until a difficulty is chosen.
What I am trying to get it to do, is loop if it doesn't select e,1,m,2,h,3,i,4 etc. I cant' manage it at the moment in switches
Ahhh, I got it.
What I did was really simple, here's the new code and it's only a minor tweak that's fixed it!
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
#include <iostream>

using namespace std;
char Difficulty;
char Acceptable = ('E' || 'e' || '1' || 'M' || 'm' || '2' || 'H' || 'h' || '3' || 'I' || 'i' || '4');
void PickDifficulty()
{
    switch (Difficulty)
    {
        case 'E':
        case 'e':
        case '1':
            cout << "You have selected: \neasy\nIf you dont win, you're seriously dumb" << endl;
            break;
        case 'M':
        case 'm':
        case '2':
            cout << "You have selected: \nMedium\nYou should win" << endl;
            break;
        case 'H':
        case 'h':
        case '3':
            cout << "You have selected: \nHard\nGood luck" << endl;
            break;
        case 'I':
        case 'i':
        case '4':
            cout << "You have selected: \nImposssible\nAre you dumb?"<< endl;
            break;
        default:
            {

            cout << "Don't try and trick me" << endl;
            cout << "Pick again" <<endl;
            cin >> Difficulty;
            PickDifficulty();
            }
}
}

int main()
{
    cout << "Welcome to guess my number!" << endl;
    cout << "The game is pretty simple, the number is between 1-100" << endl;
    cout << "Please select what difficulty you would like to use" << endl;
    cout << "Easy - 1" << endl;
    cout << "Medium - 2" << endl;
    cout << "Hard - 3" << endl;
    cout << "Impossible - 4" << endl;
    cin >> Difficulty;
    PickDifficulty();


    return 0;
}

I got rid of the while loop in the main and simple added PickDifficulty at the bottom of default so it loops :)
thanks guys!
Last edited on
You also didn't get rid of line 5. (Please read the link to see why it doesn't do what you think. http://www.cplusplus.com/faq/beginners/logic/#or-vs-select)

Glad you got it!
Topic archived. No new replies allowed.