Recently I was assigned to create a program driven by a main menu, followed by a submenu, followed by functions. Below is an attempt of mine, which does display the main menu, and from there a user must enter a value to proceed to a second menu, however it doesn't seem to work, is there anybody that can help? NOTE: The program is far from complete, so the only choice available is "1", which will select the distance conversion, but my problem is that it won't display on the screen, when it does, it should have more options.

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int MainMenu();
int menu0(int);
double DistanceConversion(string, string, double);

int main()
{
	int choosefunction = 1;
	while ( choosefunction != 5 )
	{
		do 
		{
			if (choosefunction > 5 || choosefunction <= 0)
			{
			cout << "Are you on drugs? Try Again: " ;
			cin.clear();
			fflush(stdin);
			}
		} while (choosefunction < 0 || choosefunction > 5);
		choosefunction = MainMenu();
	}
	
	if (choosefunction != 5)
	switch (choosefunction)
	{
		case 1: 
			int DistanceConversion();

	}

fflush(stdin);
cin.get();
return 0;
}
int MainMenu()
{
	system ("cls");
	int choosefunction;
	cout << "       M A I N M E N U"<< endl;
	cout <<"     PLEASE PICK AN OPTION"<<endl;
	cout << "   ________________________" << endl;
	cout << "    1 | Distance Conversion" << endl;
	cout << "    2 | Weight Conversion" << endl;
	cout << "    3 | Volume Conversion" << endl;
	cout << "    4 | Pressure Conversion" << endl;
	cout << "    5 | END PROGRAM" << endl;
	while (!(cin >> choosefunction))
	{
		cout << "Don't lie, you are using drugs. Thats not even on the Menu." << endl
		<< "Re-Enter a choice thats on the Menu:  ";
		cin.clear();
		fflush(stdin);
	}
	return choosefunction;
}
double DistanceConversion(string to, string from, double factor)
{

}
int menu0(int choosefunction)
{
	int choosefuntion;
	system("cls");
	cout << "      D I S T A N C E C O N V E R S I O N";
	while (!(cin >> choosefunction))
		{
			cout << "\n Invalid Please Re-Enter: ";
			cin.clear();
			fflush(stdin);
		}
	return choosefunction;
}

Recommended Answers

All 4 Replies

>> fflush(stdin) -- that is non-standard C code which is not supported by very many compilers. If you compiler does, ok, but don't expect the program to compile by your instructor's compiler. And that might get you a lower grade.

move line 25 up to line 18 so that the menu is displayed before testing for invalid input.

>> fflush(stdin) -- that is non-standard C code which is not supported by very many compilers. If you compiler does, ok, but don't expect the program to compile by your instructor's compiler. And that might get you a lower grade.

move line 25 up to line 18 so that the menu is displayed before testing for invalid input.

Actually I was referring to the second menu, not the first one, because that is the main menu, I want it so that after the main menu is displayed, i can enter 1, and it goes on to display the second menu.

>>while (!(cin >> choosefunction))
Wrong. Use a switch statement instead of that while statement

cin >> choosefunction;
switch(choosefunction)
{
   case '1':  
        SecondMenu();
        break;
   case '2':
        ThirdMenu();
        break;
   // etc. for each 
   default:
      cout << "What an idot you are\n";
      break;
}

Then what you need to do is use a switch, or other similar logic/control structure, to analyze what the user entered:

cout << "Here are your options:" << endl;
cout << "1.\tOption 1\n";
cout << "2.\tOption 2\n";
cout << "3.\tOption 3" << endl;
cout << "Enter an option: ";
cin >> userChoice;

switch (userChoice) {
case 1:
  //...do something 1
  break;
case 2:
  //...do something 2
  break;
case 3:
  //...do something 3
  break;
default:
  cout << "You're a dope..., try again."
}
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.