Hey there;

I've run into a problem!

I have a program that is supposed to do multiple things.
Therefore - I have a menu (switch statements).

Is there any way I can make it so when 1 statement finishes, it can go back to the main menu? I can add a case for X (10 we'll say) that will end the program.

If there isn't a way.. how should I do this menu?

Recommended Answers

All 7 Replies

use a while loop

bool done = false;
while( !done )
{
   //show menu and execute switch statements or other stuff
}

You can use other kinds of loops too, depending on what you want it to do.

int main()
{
	// Declaration of integers
	int index=0, intChoice=0, DatabaseSize=0;
	bool finished=false, Flag=false;
	// Declaration of the Array of Structs with size intIndex
	User UserA[SIZE];	
	// Install the inStream and outStream 
	ifstream inStream;
	ofstream outStream;
	
	
	// CHECK TO SEE IF FILE WILL OPEN 
	inStream.open("MasterUserFile.txt");

		if (inStream.fail())
		{
			cout << "Cannot open MasterUserFile.txt for reading, aborting" << endl;
			system("PAUSE");
			return 0;
		}
   
	while(!inStream.eof())
	{
			inStream >> UserA[index].user_id;
			inStream >> UserA[index].firstName;
			inStream >> UserA[index].lastName;
			inStream >> UserA[index].password;
			inStream >> UserA[index].priority;
			index++;
			
	}

	// After the contents has been read in, it no longer needs to be open
	inStream.close();
	
	// Menu
		
		cout << "INFR1100U final project: \n";
		cout << "\n***********MENU**************\n";
		cout << "_____________________________\n";
		cout << "Add/Remove Options\n";
		cout << "1. Add New User\n";
		cout << "2. Remove an Existing User\n";
		cout << "_____________________________\n";
		cout << "Modify existing users\n";
		cout << "3. Change Password\n";
		cout << "4. Change Priority\n";
		cout << "_____________________________\n";
		cout << "Print information\n";
		cout << "5. Print User Information\n";
		cout << "6. Print All user Information\n";
		cout << "_____________________________\n";
		cout << "Misc. Options\n";
		cout << "7.Write Records to Database\n";
		cout << "8. ";
		
		while (intChoice!=10)
		{

			cin >> intChoice;

			switch(intChoice)
			{
				case 1:
					// Prompt user
					cout <<"\nAdd User\n";
					while (!finished==true)
					{
						cout << "\nPlease enter a username: ";
						cin >> UserA[index].user_id;
						cout << "\nPlease enter the users first name: ";
						cin >> UserA[index].firstName;
						cout << "\nPlease enter the users last name: ";
						cin >> UserA[index].lastName;
						cout << "\nPlease enter the users password: ";
						cin >> UserA[index].password;
					
						// User priority check
						while(!Flag)
						{
						cout << "\nPlease enter the users priority (1-7):";
						cin>> UserA[index].priority;
							if (UserA[index].priority <=7 && UserA[index].priority  >=0)
							{
								Flag=true;
							}
						 else
							{
							cout << "Error, please enter a number that equal to 0 and greater, less than 5 and equal to 5"<<endl;
							}   
						}
						// Add one to the array for the next time.
						index++;
						finished=true;
					}														
					break;
			case 2:
					
						/*
						// CHECK TO SEE IF FILE WILL OPEN 
						outStream.open("MasterUserFile.txt");

						// Attempt to open to write.. if fail, shut down!
						if (outStream.fail())
							{
								cout <<"Cannot open MasterUserFile.txt. Aborting";
								outStream.close(); // Close 1st file if it fails
								exit(1);
							}
					
						// Declare a temporary variable to save the search query and prompt user for action
						char TempSearch;
					
						cout <<"\nRemove User - Please enter the User Name (ex. 12345)";
						cin >> TempSearch;
	
						if (!strcmp(UserA[index].user_id, "TempSearch"))
						{
							cout << "";
						}
								
						// Close the file
						outStream.close();
					}
					*/
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			case 6:
				break;
			case 7:
				break;
			case 8:
				break;

			default: "GG you broke it.";
			}
		}
		
	return 0;
}

Still ends after I finish entering a user.

You didn't put the menu in a loop as I suggested earlier.

bool done = false;
while( !done )
{
	// Menu
		
		cout << "INFR1100U final project: \n";
		cout << "\n***********MENU**************\n";
		cout << "_____________________________\n";
		cout << "Add/Remove Options\n";
		cout << "1. Add New User\n";
		cout << "2. Remove an Existing User\n";
// etc etc etc
   }

My trigger works the same way.. it still doesn't work. Is it because of the breaks?

you need to make a couple logic changes.

while(intChoice!=10)
    {
		cout << "INFR1100U final project: \n";
		cout << "\n***********MENU**************\n";
		cout << "_____________________________\n";
		cout << "Add/Remove Options\n";
		cout << "1. Add New User\n";
		cout << "2. Remove an Existing User\n";
		cout << "_____________________________\n";
		cout << "Modify existing users\n";
		cout << "3. Change Password\n";
		cout << "4. Change Priority\n";
		cout << "_____________________________\n";
		cout << "Print information\n";
		cout << "5. Print User Information\n";
		cout << "6. Print All user Information\n";
		cout << "_____________________________\n";
		cout << "Misc. Options\n";
		cout << "7.Write Records to Database\n";
		cout << "8. ";

			cin >> intChoice;

			switch(intChoice)
			{
				case 1:
					// Prompt user
					cout <<"\nAdd User\n";
					while (!finished)
					{
						cout << "\nPlease enter a username: ";
						cin >> UserA[index].user_id;
						cout << "\nPlease enter the users first name: ";
						cin >> UserA[index].firstName;
						cout << "\nPlease enter the users last name: ";
						cin >> UserA[index].lastName;
						cout << "\nPlease enter the users password: ";
						cin >> UserA[index].password;
					
						// User priority check
						while(!Flag)
						{
						cout << "\nPlease enter the users priority (1-7):";
						cin>> UserA[index].priority;
							if (UserA[index].priority <=7 && UserA[index].priority  >=0)
							{
								Flag=true;
							}
						 else
							{
							cout << "Error, please enter a number that equal to 0 and greater, less than 5 and equal to 5"<<endl;
							}   
						}
						// Add one to the array for the next time.
						index++;
						finished=true;
					}														
					break;
			case 2:
					
						/*
						// CHECK TO SEE IF FILE WILL OPEN 
						outStream.open("MasterUserFile.txt");

						// Attempt to open to write.. if fail, shut down!
						if (outStream.fail())
							{
								cout <<"Cannot open MasterUserFile.txt. Aborting";
								outStream.close(); // Close 1st file if it fails
								exit(1);
							}
					
						// Declare a temporary variable to save the search query and prompt user for action
						char TempSearch;
					
						cout <<"\nRemove User - Please enter the User Name (ex. 12345)";
						cin >> TempSearch;
	
						if (!strcmp(UserA[index].user_id, "TempSearch"))
						{
							cout << "";
						}
								
						// Close the file
						outStream.close();
					}
					*/
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			case 6:
				break;
			case 7:
				break;
			case 8:
				break;

			default: "GG you broke it."; break;
			}
//		}
    }		
	return 0;
}

Where what? All I did was change what you posted and moved that while statement to the top of the menu.

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.