I'm working on a program driven by a menu. At the end of each menu component, the user will have the option of running that component again. If they enter an invalid entry (not a y or n) I want to go back to the main menu. I shortened my code below to just show the relevant parts. The problem I'm having is I can't get the entire program to loop through again. After the invalid entry, the user is taken back to the main screen and inputs a selection, but then the program stops it doesnt continue on with that selection.

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

//function prototypes
char getSelection();
void displayThankYou();

int main()
{//Begin main function

    //declare variables
    string name;
    char selection; 
    double items = 0;
    char another = 'Y';
    char answer = 'Y';

    do
    {//do while for default switch

    selection = getSelection();

    switch (selection)
    {
        case '1':
            do
            {//Begin do-while function to run program again

                //... code goes here

                cout << "Would you like to run the receipt program again? (Y/N) : ";
                cin >> another;
                }while(toupper(another)=='Y');
                if(toupper(another)=='N')
                {
                    displayThankYou();
                    return 0;
                }
                else if(toupper(another)!='Y'|| another != 'N')
                {
                    selection = getSelection();
                }

            return 0;
            break;

    default:
        cout << "***************************************" << endl;
        cout << "***************************************" << endl;
        cout << "******   T H A T  W A S  N O T   ******" << endl;
        cout << "****** A  V A L I D  O P T I O N ******" << endl;
        cout << "***************************************" << endl;
        cout << "***************************************" << endl << endl;
        cout << "Would you like to return to the main menu? (Y/N): " ;
        cin >> answer;
    } //end switch
    }while(toupper(answer)=='Y');

return 0;
}//end of main function



//*******************function definitions

char getSelection()
{
    char choice;
    cout << "Option          Description" << endl;
    cout << "---------------------------" << endl;
    cout << "   1            Create a sales receipt"<< endl;
    cout << "   2            Create a customer contact card" << endl;
    cout << "   3            Balance a checkbook"<< endl;
    cout << "   4            Create a trip mileage report"<< endl;
    cout << "   0            Exit program" << endl << endl;
    cout << "What is your selection?";
    cin >> choice;
    return choice;
    cout << endl << endl << endl;

}

void displayThankYou()
{
    cout << "**************************************" << endl;
    cout << "**************************************" << endl;
    cout << "*******    T H A N K  Y O U    *******" << endl;
    cout << "*******    F O R  U S I N G    *******" << endl;
    cout << "******* T H I S  P R O G R A M *******" << endl;
    cout << "**************************************" << endl;
    cout << "**************************************" << endl;
}

If they enter an invalid entry (not a y or n) I want to go back to the main menu.

You want them to go back to the main menu if they enter an invalid entry OR if they enter a 'Y'(that was the original question, after all).

"Would you like to return to the main menu? (Y/N): "

In other words, they should return to the main menu if they enter anything but an 'N'.

So test for that. Change line 60 to test whether they typed an 'N'. If not, repeat the main menu, which means repeat the loop.

The part where they enter a y/n is if they want to run the program again. If they enter a y, it goes through that program again. If they enter a n, they get a thank you screen. I'm tryingn to get it for when they don't enter y/n they go back to the main menu. The specific part where I'm trying to do this is:

>

 else if(toupper(another)!='Y'|| another != 'N')
>                 {
>                     selection = getSelection();
>                 }

(lines 42-45)

Now at this part all it does is go to the menu, get a selection, then quit.. instead of proceeding through with their selection.

You have two separate Yes or No questions. Which one is causing problems? I was under the impression that ypu were having problems around line 58 when the user enters 'Z' or some other nonsensical answer. When that occurs, you want it treated as if they entered 'Y'. You want the menu to repeat. Currently the progeam will terminate if 'Z' is entered. You want the lines 21 to 60 loop to repeat upon an invalid entry, correct? Hence my suggestion.

I see two char variables that take a 'Y' or an 'N': another and answer. Why two variables? One seems sufficient. Menus normally work like this. Options are listed along with the code to enter. One of the options is the quit option and you test this in the do-while loop. If an invalid option is entered, you stick that in the default case, as you have, print an error message, and reprint the menu by repeating the loop.

Line 47 screws everything up and ends the program. You have no chance of recovering from bad input with that "return 0". You have a legitimate exit from the program on line 40. That's sufficient. Lines 44 and 47 execute only when they type something in error. Hence delete line 47 for the reason mentioned. Line 44 should contain whatever error message you want printed for bad input. The break statement on line 48 should get them back to the main menu.

Also, the "else if" statement on line 42 seems redundant. Seems like it will always be true. You've already taken care of the case where another is 'Y' or 'N' above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.