C++ Loop help to restart to beginning of program

I apologize for the redundancy, as I am new to C++ and I am sure it has been answered before. I am asking in the console application if they need to re-pick their choice and go back to the beginning. I am trying to utilize a "do while" loop. If they enter "y" or "Y" it will say they are being redirected to the beginning of the program. If they enter "n" or "N" they move on to the next part of the program. I will just call the program "Homework". Every time I do the "do while" loop it will keep saying "Your game is initializing.", when I input "n" or "N" and is still doing a loop for that until I enter in "y" or "Y" and even then it keeps saying the same thing. If I take away the "do while" loop I can get "n" or "N" to say "Your game is initializing." and if I enter "y" or "Y" I can get it to say "Restarting system...". I am trying to get the input of "y" or "Y" to actually restart the program to the very beginning while letting it say "Restarting system...". I am only a week in, so please break it down for me and explain what I am doing wrong and what I need to do to make it right (and why please). The comments at the bottom is what I don't want to delete and forget yet. Thank you in advance! What I have right now will be shown as follows:

#include <iostream>
#include <string>

using namespace std;

int main()
{
enum mode { EASY, NORMAL, HARD, EXPERT, BRUTAL, IMPOSSIBLE };

cout << "Enter the cooresponding menu number and press enter " << endl;
cout << endl;
cout << "What difficulty do you want to play? " << endl;
cout << endl;
cout << "0. Easy Mode " << endl;
cout << "1. Normal Mode " << endl;
cout << "2. Hard Mode " << endl;
cout << "3. Expert Mode " << endl;
cout << "4. Brutal Mode " << endl;
cout << "5. Impossible Mode " << endl;
cout << endl;

int mode = 0;

cin >> mode;
cout << endl;

switch (mode)
{
case EASY:
cout << "Easy Mode was selected" << endl;
break;

case NORMAL:
cout << "Normal Mode was selected" << endl;
break;

case HARD:
cout << "Hard Mode was selected" << endl;
break;

case EXPERT:
cout << "Expert Mode was selected" << endl;
break;

case BRUTAL:
cout << "Brutal Mode was selected" << endl;
break;

case IMPOSSIBLE:
cout << "Impossible Mode was selected" << endl;
break;
}

cout << endl;
cout << "Would you like to start over? " << endl;
cout << "Please input: Y or N: " << flush;

char result1;
cin >> result1;
cout << endl;

do
{

if (result1 != 'y' || result1 != 'Y');
{cout << "Thank You, your game is initializing." << endl;
cin >> result1;
}

} while (result1 != 'n' || result1 != 'N');



//if(result1 == 'y' || result1 == 'Y')
//{
// cout << "Restarting System..." << endl;
//}
//else
//{
// cout << "Thank You, your game is initializing." << endl;
//}
//while(result1 == 'y' || result1 == 'Y')
//{
// cout << "Restarting System..." << endl;
//}

return 0;
}
while (result1 != 'n' || result1 != 'N');

Consider if result1 is 'n', the loop will still fire because it is not 'N', due to the ||

Try &&

Or, better, transform the case so there's only 1 'n' case to test.
@kasper22,

Try this code & let me know whether you get the desired result?

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
92
#include <iostream>
#include <string>

using namespace std;

int main()
{
enum mode { EASY, NORMAL, HARD, EXPERT, BRUTAL, IMPOSSIBLE };

cout << "Enter the cooresponding menu number and press enter " << endl;
cout << endl;
cout << "What difficulty do you want to play? " << endl;
cout << endl;
cout << "0. Easy Mode " << endl;
cout << "1. Normal Mode " << endl;
cout << "2. Hard Mode " << endl;
cout << "3. Expert Mode " << endl;
cout << "4. Brutal Mode " << endl;
cout << "5. Impossible Mode " << endl;
cout << endl;

int mode = 0;

cin >> mode;
cout << endl;

switch (mode)
{
case EASY:
cout << "Easy Mode was selected" << endl;
break;

case NORMAL:
cout << "Normal Mode was selected" << endl;
break;

case HARD:
cout << "Hard Mode was selected" << endl;
break;

case EXPERT:
cout << "Expert Mode was selected" << endl;
break;

case BRUTAL:
cout << "Brutal Mode was selected" << endl;
break;

case IMPOSSIBLE:
cout << "Impossible Mode was selected" << endl;
break;
}

cout << endl;
cout << "Would you like to start over? " << endl;
cout << "Please input: Y or N: " << flush;

char result1;
cin >> result1;
cout << endl;

do
{

if (result1 == 'y' || result1 == 'Y');
{
    break;
    cout << "Thank You, your game is initializing." << endl;
cin >> result1;
}

} while (result1 == 'n' || result1 == 'N');



if(result1 == 'y' || result1 == 'Y')

{
 cout << "Restarting System..." << endl;
}
else
{
 cout << "Thank You, your game is initializing." << endl;
}
/*while(result1 == 'y' || result1 == 'Y')
{
 cout << "Restarting System..." << endl;
}
*/

return 0;
}
Last edited on
@Niccolo I tried that as well, I guess I had something wrong elsewhere.

@zarrar12 I tried the code and that's the result I am looking for, except I am trying to make the program restart back to the beginning if they say "Y" (Yes) to restarting the system. I don't know how to do that exactly. I emailed my teacher this morning and he told me it would need to execute the menu part with the loop. We are learning loops on Monday, so he doesn't want to give me any info much until then so I don't get confused, but I am learning on my own.
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>
#include <cctype>
using namespace std;

int main() {
    enum mode { EASY, NORMAL, HARD, EXPERT, BRUTAL, IMPOSSIBLE, NUM_MODES };
    const char *modestr[] =
        { "Easy", "Normal", "Hard", "Expert", "Brutal", "Impossible" };

    for (char loop = 'Y'; loop == 'Y'; ) {
        cout << "What difficulty do you want to play?\n\n";
        for (int i = 0; i < NUM_MODES; ++i)
            cout << i << ". " << modestr[i] << " Mode\n";

        cout << "\nEnter mode: ";
        int mode = 0;
        cin >> mode;

        if (mode >= 0 && mode < NUM_MODES)
            cout << '\n' << modestr[mode] << " Mode was selected\n";
        else
            cout << "\nBad mode selected\n";

        cout << "\nWould you like to start over?\n";
        do {
            cout << "Input: Y or N: ";
            cin >> loop;
            loop = toupper(loop);
        } while (loop != 'Y' && loop != 'N');
    }
}

Last edited on
@dutch Thank you! That's exactly what I was trying to figure out. Would you mind explaining to me what you did that I needed to do, as I am seeing some code that I haven't encountered yet. IE (NUM_MODES, modestr, toupper(loop) (I'm guessing that is sending it back up to the for(char loop)??? Sorry for asking questions and sounding like a noob, I just want to get the most that I can.
@kasper22, some of these are probably intended for you to look up.

For example, toupper. This function returns a string where the input is transformed to upper case letters, so 'y' becomes 'Y' and 'n' becomes 'N', simplifying the tests.

modestr is an array of character arrays (one might say array of strings) of the modes, now tied to mode numbers (the nth entry in the array is the name of that mode).

NUM_NODES is the last enum in the list, and since enums 'count automatically', it happens to define how long the enum list is.

Topic archived. No new replies allowed.