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.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
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.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18
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.
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18