Creating A Menu System In Visual C++

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 26617 Views - Last Post: 07 April 2005 - 12:35 PM Rate Topic: -----

#1 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Creating A Menu System In Visual C++

Posted 19 February 2005 - 12:13 PM

Hi ho y'all.

Now, this is probably going to sound extremely stupid, seeing as i've only just started my programming module at university.

We've been given a basic outline of a program we have to create, and one of the basic stipulations, is that there be a menu system.

The menu system has 3 options, one of which is exit - so i'm assuming that 'break;' will take care of that. However, how on earth do i take an input and use that input for executing a function?

I've tried playing with the switch statement, but it never wants to compile, and throws up craploads of errors every time I try and use it.

I've included the code i've got so far for the whole program, and I would appreciate any help you could give me.

#include <iostream>
using namespace std;

void welcome_screen()

{
cout << "\t***************************************************************\n";
cout << "\t* *\n";
cout << "\t* Welcome to the TravelWithUs Booking and Information System *\n";
cout << "\t* *\n";
cout << "\t***************************************************************\n";
cout << endl;
}

/***************************************************************************************/

void menu_system()

{
char selection = ' ';

cout << endl;
cout << "The following options are available:\n";
cout << endl;
cout << "\tC. Country Information\n";
cout << "\tT. Travel Cost Information\n";
cout << "\tE. Exit";
cout << endl;
cout << endl;
cout << "Please choose an option: ";
cin >> selection;
cout << "You typed: " << selection;
}

/***************************************************************************************/

/***************************************************************************************/
int main()
{
welcome_screen();
menu_system();
return (0);
}

Bear in mind that for some daft reason, int main() has to be at the bottom of the C++ code, maybe our tutor is obsessive compulsive or something :D

TIA :)

Is This A Good Question/Topic? 0
  • +

Replies To: Creating A Menu System In Visual C++

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Creating A Menu System In Visual C++

Posted 19 February 2005 - 12:33 PM

See my comments in the code below
#include <iostream>
using namespace std;

void welcome_screen()

{
cout << "\t***************************************************************\n";
cout << "\t* *\n";
cout << "\t* Welcome to the TravelWithUs Booking and Information System *\n";
cout << "\t* *\n";
cout << "\t***************************************************************\n";
cout << endl;
}

/***************************************************************************************/

char menu_system()//the return type has been changed to a char

{
char selection = ' ';

cout << endl;
cout << "The following options are available:\n";
cout << endl;
cout << "\tC. Country Information\n";
cout << "\tT. Travel Cost Information\n";
cout << "\tE. Exit";
cout << endl;
cout << endl;
cout << "Please choose an option: ";
cin >> selection;
cout << "You typed: " << selection;
return selection;//this returns the selection to the calling function
}

void processChoice(char userSelection)//this will be your process function
{
}

/***************************************************************************************/

/***************************************************************************************/
int main()
{
char userSelection;//this will store the users selection
welcome_screen();
userSelection = menu_system();
processChoice(userSelection);//this will pass the selection to your processing function
return (0);
}


Was This Post Helpful? 0
  • +
  • -

#3 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Re: Creating A Menu System In Visual C++

Posted 19 February 2005 - 12:55 PM

Thank you for responding so quickly!

I've compiled it with your changes, no problems there - now comes the hard part - how do I process the inputted character? Apologies if this sounds stupid - but I'm really at the bum end of the programming spectrum!

Should I use switch, an if/else or is there another way of doing this?

This post has been edited by Confused-Student: 19 February 2005 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Creating A Menu System In Visual C++

Posted 19 February 2005 - 01:03 PM

There are many ways to accomplish this...here is an example with a switch statement:
void processChoice(char userSelection)
{
	switch(userSelection)
	{
	case 'C':
  //do something
  break;
	case 'T':
  //do something
  break;
	case 'E':
  //exit or break here, whatever you want
	default:
  cout<<"You've inputted an incorrect character...Please try again"<<endl;
	}

}


Please note that you should always check any user input for validity. I have not done that here.
Was This Post Helpful? 0
  • +
  • -

#5 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Re: Creating A Menu System In Visual C++

Posted 19 February 2005 - 01:12 PM

Excellent! I Think i can figure out the rest from here, although two minor questions.

Firstly, is there a way of specifying that both 'c' and 'C' will execute the same switch?

Secondly, in the 'default' - is there a way of making it run the function from scratch, rather than just saying 'error' and exiting? I tried adding:

void processChoice();

after the cout, but it didn't seem to do anything, although it compiled fine.

Again, many thanks for the assistance :)
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Creating A Menu System In Visual C++

Posted 19 February 2005 - 01:25 PM

It didn't do anything because you are not passing the processChoice function a value...even if you were, you'd be passing it the same value that caused the default condition, and creating an endless loop.

The preferred method of doing this would be to run a loop in your main, and make the necessary adjustments in the functions.

As for the chars, you can either put in additional case statements to include the lower cases, you can use if/else statements to use multiple conditions, or you can convert the incoming user input to upper case in the function
Was This Post Helpful? 0
  • +
  • -

#7 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Re: Creating A Menu System In Visual C++

Posted 19 February 2005 - 04:30 PM

Right - it's taken me a while - but I think i've actually got it.

So far, i've adapted the code you gave me, and i've expanded on it to give me a submenu. Now, the problem is, that under the submenu system, when i input an option, the function seems to run again, and only responds with the correct output after i enter the option again. I'm sure this is likely to be something stupid...

Also stupid, is when I enter 't' or 'T' and the program goes to country_info, rather than going to travel_cost - is the clue to this in 'int main()' like I think it is?

I managed to use 'toupper' to convert any lower case input to upper case, which the program is expecting.

The source is a little on the long side, but here goes:

#include <iostream>
#include <stdlib.h>
#include <cctype>

using namespace std;

void welcome_screen()

{
	system("CLS");
	cout << "\t**************************************************************\n"; 
	cout << "\t*                                                            *\n";
	cout << "\t* Welcome to the TravelWithUs Booking and Information System *\n";
	cout << "\t*                                                            *\n";
	cout << "\t**************************************************************\n";
	cout << endl;
}

/***************************************************************************************/

char country_info()
{
	system("CLS");
	cout << "\t**************************************************************\n"; 
	cout << "\t*                                                            *\n";
	cout << "\t*                    Country Information                     *\n";
	cout << "\t*                                                            *\n";
	cout << "\t**************************************************************\n";
	cout << endl;

	char selection2 = ' ';

	cout << endl;
	cout << "We currently offer the following destinations:\n";
	cout << endl;
	cout << "\t1. France\n";
	cout << "\t2. Spain\n";
	cout << "\t3. Portugal\n";
	cout << "\t4. Italy\n";
	cout << "\t5. Germany\n";
	cout << endl;
	cout << endl;
	cout << "Please choose a country: ";
  
	cin >> selection2;

	return selection2;
}

/***************************************************************************************/

void travel_cost()
{
	cout << "Moose.";
}

/***************************************************************************************/

char menu_system()

{
	char selection1 = ' ';

	cout << endl;
	cout << "The following options are available:\n";
	cout << endl;
	cout << "\tC. Country Information\n";
	cout << "\tT. Travel Cost Information\n";
	cout << "\tE. Exit";
	cout << endl;
	cout << endl;
	cout << "Please choose an option: ";
  
	cin >> selection1;
	selection1 = toupper(selection1);

	return selection1;
}

/***************************************************************************************/

void processChoice1 (char userSelection1)
{
	switch(userSelection1)
	{
  case 'C':
   country_info();
   break;
  case 'T':
   travel_cost();
   break;
  case 'E':
   break;
  default:
   cout << "Incorrect Option. Please choose again." << endl;
	}

}

/***************************************************************************************/

void processChoice2 (char userSelection2)
{
	switch(userSelection2)
	{
  case '1':
   cout << "Option 1\n";
   break;
  case '2':
   cout << "Option 2\n";
   break;
  case '3':
   cout << "Option 3\n";
   break;
  case '4':
   cout << "Option 4\n";
   break;
  case '5':
   cout << "Option 5\n";
   break;
  default:
   cout << "Incorrect Option. Please choose again." << endl;
	}

}

/***************************************************************************************/

int main()
{
	char userSelection1;
	char userSelection2;
	welcome_screen();
	userSelection1 = menu_system();
	userSelection2 = country_info();
	processChoice1(userSelection1);
	processChoice2(userSelection2);
	return (0);
}



Am I going about this the right way, or am I overcomplicating things?

This post has been edited by Confused-Student: 19 February 2005 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#8 Videege  Icon User is offline

  • rvant.toujours
  • member icon

Reputation: 6
  • View blog
  • Posts: 1,413
  • Joined: 25-March 03

Re: Creating A Menu System In Visual C++

Posted 20 February 2005 - 11:15 AM

The problem, like you thought, lies within main();.

I'll post it here real quick:

int main()
{
char userSelection1;
char userSelection2;
welcome_screen();
userSelection1 = menu_system();
userSelection2 = country_info();
processChoice1(userSelection1);
processChoice2(userSelection2);
return (0);
}



You see, after you set userSelection1 to whatever menu_system returns, you then call country_info() right after it. So it really doesn't matter what they input from menu_system because you don't process input until later. You'll probably need to remove the call to country_info from main, and only call it if in processChoice(userSelection1) the user chooses to go to country_info. Hmm..in short, just get rid of the whole userSelection2/processChoice2 bit and put it elsewhere in your program based off the first processChoice. Sorry if I'm confusing.

Also, you will need to run the functions you have in main in a while loop if you want them to be able to return to the main menu until they choose to exit. this could be done in this manner:

int main()
{
while (!done)
{
//do stuff here
}
}



Where done is a global boolean (default is false). You said in your first post that a break; will suffice, but unfortunately, a break ony exits the code block it is currently in (in your case, the switch statement). You should instead set the done variable to true when the user inputs E. Your program will then exit.

This post has been edited by Videege: 20 February 2005 - 11:20 AM

Was This Post Helpful? 0
  • +
  • -

#9 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Re: Creating A Menu System In Visual C++

Posted 20 February 2005 - 11:51 AM

Thanks for the reply. So, basically, i obliterate the processChoice2 and associated from int main(), but where should i put it, if it needs to be put anywhere at all?

The looped main() is very helpful, and i'll have a go at that later on, with something simpler to get the hang of it.
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Creating A Menu System In Visual C++

Posted 20 February 2005 - 11:55 AM

void processChoice1 (char userSelection1)
{
char userSelction2;
switch(userSelection1)
{
  case 'C':
   userSelction2 = country_info();
   processChoice2(userSelection2);
   break;
  case 'T':
   travel_cost();
   break;
  case 'E':
   break;
  default:
   cout << "Incorrect Option. Please choose again." << endl;
}

}


Was This Post Helpful? 0
  • +
  • -

#11 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Re: Creating A Menu System In Visual C++

Posted 20 February 2005 - 02:28 PM

I must seem so thick by comparison to the rest of y'all :)

I'm assuming that I can keep re-using that same formula over and over for each sub-menu?
Was This Post Helpful? 0
  • +
  • -

#12 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Re: Creating A Menu System In Visual C++

Posted 20 February 2005 - 02:31 PM

Erp - just tried compiling that - and VC++ threw up the following:

------ Build started: Project: test1, Configuration: Debug Win32 ------
Compiling...
test1.cpp
c:\Documents and Settings\Kai Robinson\My Documents\Visual Studio\Projects\test1\test1\test1.cpp(90) : error C3861: 'processChoice2': identifier not found
c:\Documents and Settings\Kai Robinson\My Documents\Visual Studio\Projects\test1\test1\test1.cpp(106) : error C2365: 'processChoice2' : redefinition; previous definition was 'formerly unknown identifier'
Build log was saved at "file://c:\Documents and Settings\Kai Robinson\My Documents\Visual Studio\Projects\test1\test1\Debug\BuildLog.htm"
test1 - 2 error(s), 0 warning(s)



Now, i'll include the whole program again, just so you can see what's been added etc.

#include <iostream>
#include <stdlib.h>
#include <cctype>

using namespace std;

void welcome_screen()

{
	system("CLS");
	cout << "\t**************************************************************\n"; 
	cout << "\t*                                                            *\n";
	cout << "\t* Welcome to the TravelWithUs Booking and Information System *\n";
	cout << "\t*                                                            *\n";
	cout << "\t**************************************************************\n";
	cout << endl;
}

/***************************************************************************************/

char country_info()
{
	system("CLS");
	cout << "\t**************************************************************\n"; 
	cout << "\t*                                                            *\n";
	cout << "\t*                    Country Information                     *\n";
	cout << "\t*                                                            *\n";
	cout << "\t**************************************************************\n";
	cout << endl;

	char selection2 = ' ';

	cout << endl;
	cout << "We currently offer the following destinations:\n";
	cout << endl;
	cout << "\t1. France\n";
	cout << "\t2. Spain\n";
	cout << "\t3. Portugal\n";
	cout << "\t4. Italy\n";
	cout << "\t5. Germany\n";
	cout << endl;
	cout << endl;
	cout << "Please choose a country: ";
  
	cin >> selection2;

	return selection2;
}

/***************************************************************************************/

void travel_cost()
{
	cout << "Moose.";
}

/***************************************************************************************/

char menu_system()

{
	char selection1 = ' ';

	cout << endl;
	cout << "The following options are available:\n";
	cout << endl;
	cout << "\tC. Country Information\n";
	cout << "\tT. Travel Cost Information\n";
	cout << "\tE. Exit";
	cout << endl;
	cout << endl;
	cout << "Please choose an option: ";
  
	cin >> selection1;
	selection1 = toupper(selection1);

	return selection1;
}

/***************************************************************************************/

void processChoice1 (char userSelection1)
{
	char userSelection2;

	switch(userSelection1)
	{
  case 'C':
 	 userSelection2 = country_info();
 	 processChoice2(userSelection2);
 	 break;
  case 'T':
 	 travel_cost();
 	 break;
  case 'E':
 	 break;
  default:
 	 cout << "Incorrect Option. Please choose again." << endl;
	}

}

/***************************************************************************************/

void processChoice2 (char userSelection2)
{
	switch(userSelection2)
	{
  case '1':
 	 cout << "Option 1\n";
 	 break;
  case '2':
 	 cout << "Option 2\n";
 	 break;
  case '3':
 	 cout << "Option 3\n";
 	 break;
  case '4':
 	 cout << "Option 4\n";
 	 break;
  case '5':
 	 cout << "Option 5\n";
 	 break;
  default:
 	 cout << "Incorrect Option. Please choose again." << endl;
	}

}

/***************************************************************************************/

int main()
{
	char userSelection1;
	welcome_screen();
	userSelection1 = menu_system();
	processChoice1(userSelection1);
	return (0);
}


This post has been edited by Confused-Student: 20 February 2005 - 02:32 PM

Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Creating A Menu System In Visual C++

Posted 20 February 2005 - 02:51 PM

You're defining your functions above the main function as opposed to using function prototypes. This means that you'll have to define the functions before they can be used. Since processChoice1 calls processChoice2, it has to be defined before process choice 1. Move processChoice2 above processChoice 1 and recompile.
Was This Post Helpful? 0
  • +
  • -

#14 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Creating A Menu System In Visual C++

Posted 20 February 2005 - 02:53 PM

Confused-Student, on Feb 20 2005, 04:28 PM, said:

I must seem so thick by comparison to the rest of y'all :)

I'm assuming that I can keep re-using that same formula over and over for each sub-menu?

Not at all!! Like anything, programming takes a little time to learn. You seem to be getting the concepts quite well, you'll probably pick it up quite quickly. :)
Was This Post Helpful? 0
  • +
  • -

#15 Confused-Student  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 19-February 05

Re: Creating A Menu System In Visual C++

Post icon  Posted 24 February 2005 - 02:46 PM

Right - just as an update - this is what the code looks like now - a LOT larger for a start!

#include <iostream>
#include <stdlib.h>
#include <cctype>

using namespace std;

void welcome_screen()

{
	system("CLS");
	cout << "\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n"; 
	cout << "\tº                                                            º\n";
	cout << "\tº Welcome to the TravelWithUs Booking and Information System º\n";
	cout << "\tº                                                            º\n";
	cout << "\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n";
	cout << endl;
}

/***************************************************************************************/

char country_info()
{
	system("CLS");
	cout << "\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n"; 
	cout << "\tº                                                            º\n";
	cout << "\tº                    Country Information                     º\n";
	cout << "\tº                                                            º\n";
	cout << "\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n";
	cout << endl;

	char selection2 = ' ';

	cout << endl;
	cout << "We currently offer the following destinations:\n";
	cout << endl;
	cout << "\t1. France\n";
	cout << "\t2. Spain\n";
	cout << "\t3. Portugal\n";
	cout << "\t4. Italy\n";
	cout << "\t5. Germany\n";
	cout << endl;
	cout << "Please choose a country: ";
  
	cin >> selection2;

	return selection2;
}

/***************************************************************************************/

void travel_cost()
{
	system("CLS");
	cout << "\tÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»\n"; 
	cout << "\tº                                                            º\n";
	cout << "\tº                  Travel Cost Information                   º\n";
	cout << "\tº                                                            º\n";
	cout << "\tÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ\n";
	cout << endl;

	float VAT = 1.125;

	double ticket_price = 0.0;
	int travellers = 0;

	cout << "\tPlease enter price of air ticket: ";
	
	cin >> ticket_price;
	
	cout << endl;
	cout << endl;
	
	cout << "\tPlease enter number of travellers: ";
	cin >> travellers;

	cout << endl;
	cout << endl;

	if ( travellers > 10)
	{
  cout << "Group travel discount may be available, please contact our sales department.\n";
  cout << "Calculating airfare...\n";
  cout << endl;
	}
	
	else
	{
  cout << "Calculating airfare...\n";
  cout << endl;
	}

	cout << "Total Travel cost: œ " << (( ticket_price * travellers ) * VAT ) << endl;

}

/***************************************************************************************/

char menu_system()

{
	char selection1 = ' ';

	cout << endl;
	cout << "The following options are available:\n";
	cout << endl;
	cout << "\tC. Country Information\n";
	cout << "\tT. Travel Cost Information\n";
	cout << "\tE. Exit";
	cout << endl;
	cout << endl;
	cout << "Please choose an option: ";
  
	cin >> selection1;
	selection1 = toupper(selection1);

	return selection1;
}

/***************************************************************************************/

void processChoice2 (char userSelection2)
{
	switch(userSelection2)
	{
  case '1':
 	 cout << "Option 1\n";
 	 break;
  case '2':
 	 cout << "Option 2\n";
 	 break;
  case '3':
 	 cout << "Option 3\n";
 	 break;
  case '4':
 	 cout << "Option 4\n";
 	 break;
  case '5':
 	 cout << "Option 5\n"; 
 	 break;
  default:
 	 cout << "Incorrect Option. Please choose again." << endl;
	}

}

/***************************************************************************************/

void processChoice1 (char userSelection1)
{
	char userSelection2;

	switch(userSelection1)
	{
  case 'C':
 	 userSelection2 = country_info();
 	 processChoice2(userSelection2);
 	 break;
  case 'T':
 	 travel_cost();
 	 break;
  case 'E':
 	 cout << "Thank you for visiting TravelWithUs";
 	 break;
  default:
 	 cout << "Incorrect Option. Please choose again." << endl;
	}

}

/***************************************************************************************/

int main()
{
	char userSelection1;
	welcome_screen();
	userSelection1 = menu_system();
	processChoice1(userSelection1);
	return (0);
}



Now, since you mentioned function prototypes - what are they, and how could they be used to improve this program so far?

Also, I wanted to include an option under the country menu to exit back to the main menu, calling menu_system(), but when i did that, even though it brought the menu system up, none of the options worked, and it just quit the program....

Any ideas?

Cheers and TIA :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2