Thread: C++ Loop help to restart to beginning of program

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    1

    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:





    Code:
    #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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,408
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use a loop to restart a program
    By ben1355 in forum C Programming
    Replies: 11
    Last Post: 02-23-2015, 06:49 AM
  2. Restart the c++ program
    By Alban in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2013, 03:26 AM
  3. How to restart a program using loop?
    By NewbieCProgramm in forum C Programming
    Replies: 4
    Last Post: 12-05-2011, 10:41 AM
  4. restart program
    By kellymart87 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2007, 11:18 PM
  5. return to the beginning (restart)
    By hallo007 in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2006, 01:58 PM

Tags for this Thread