Hello all,

I'm still new at C++ and I'm almost through with this assignment. The thing I'm trying to do it have it repeat the hangmanMenu (which are switch statements) until this user chooses to exit, in which the program will exit.

Can I get some ideas on how to do this properly? Right now, I have a while loop with a loopCount variable initialized to 0. The way I have the loop, currently, is incorrect because although it repeats the hangmanMenu like I want it to, when I choose to Quit, it continues to cycle through the choices.

It's a multiple file type program, but the file in question is hangmanMenu.cpp.


#include "HangmanMenu.h"
#include <iostream>
#include <cctype>

using namespace std;

int HiScore;
int loop = 0;
//Variables declared

void HangmanMenu::showMenu()
	cout <<"\n\nGAME MENU:\n" <<endl;
	//Overrides 'base class' showMenu() definition

char HangmanMenu::getSelection(char selection)
int loopCount = 0;
while (loop !=1 && loopCount >= 0)    //<=== unsure how to make the loopCount work correctly to loop through menu but exit on choice D.
	    cout <<"\n\nPlease enter your selection: ";
	    cin >> selection;
	    selection = toupper(selection); 
	    //Converts to upper case if user enters selectio in lower case		
		switch (selection)  //Switch statements that houses output based on user selection
			case 'A':
				 cout <<"\nYou have selected New Game\n";    //no functionality yet
				 loop = 1;
			case 'B':
				 cout <<"\nYou have selected Load Game\n";   //no functionality yet
				 loop = 1;
			case 'C':
				 loop = 1;
			case 'D':
				 cout <<"Press any key to quit";
				 loop = 1;				
				cout <<"\nERROR: Invalid selection\n";
				//getSelection() definition with while loop that validates user selection by aloop variable					
return selection;	

int HangmanMenu::showHiScore(int HiScore)
         HiScore = 1000;
	cout <<"\nThe High Score is: "<<HiScore <<endl;
	//Displays HiScore

	return HiScore;

Do something like this :

enum MenuStatus{ PLAY , EXIT }; //add more stuff here if needed
MenuStatus mainMenu(){ /* menu code goes here */ }
int main(){
 while(mainMenu() != EXIT) continue;
 else { exitApp(); }
Be a part of the DaniWeb community

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