Thread: Return to menu after switch executes

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    48

    Return to menu after switch executes

    Hey,

    I'm writing a program that contains several programs operated through a switch statement. The user is prompted with a menu and chooses which program to execute. My problem is, is there a way to return to the menu after the switch's case is executed? Here's a look at the code:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    void introduction();
    string greeting();
    
    // Beginning of MAIN program.
    
    int main()
    {
    
    // Displays the introduction.
    
    	introduction();
    
    // Declaring the variables.
    
    	string	contact_first, contact_last, contact_address, contact_city;
    	string	contact_state, contact_zip, contact_phone, f_i, l_i;
    	string	full_address, full_name;
    
    	string name;
    	char menu_option;
    
    	name = greeting();
    
    // Displays the menu options.
    
    	for (int i = 0; i <= 1; i++)
    		getchar();
    	
    	char run_again;
    	do
    	{
    
    	system("cls");
    
    	cout << "****************************************************" << endl;
    	cout << "************  F I N A L  P R O G R A M  ************" << endl;
    	cout << "****************************************************" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "* PROGRAM          DESCRIPTION                     *" << endl;
    	cout << "* ------------------------------------------------ *" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "*   1              Calculate Sales Tax             *" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "*   2              Create Customer Contact Card    *" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "*   3              Balance a Checkbook             *" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "*   4              Display Trip Mileage Report     *" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "*   0              EXIT                            *" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "* ------------------------------------------------ *" << endl;
    	cout << "*                                                  *" << endl;
    	cout << "****************************************************" << endl;
    
    	cout << '\n' << "Please enter your selection: ";
    	cin >> menu_option;
    
    	cin.clear();
    	cin.sync();
    
    	switch(menu_option)
    	{
    	case '1':
    		cout << "TEST 1";
    		return(0);
    		break;
    
    	case '2':
    		do
    		{
    		cin.clear();
    		cin.sync();
    		system("cls");
    		cout << "***************************************************" << endl;
    		cout << "***** C U S T O M E R  C O N T A C T  C A R D *****" << endl;
    		cout << "***************************************************" << endl;
    
    		cout << endl << "First Name             : ";
    		getline(cin, contact_first);
    		cout << endl << "Last Name              : ";
    		getline(cin, contact_last);
    		cout << endl << "Street Address         : ";
    		getline(cin, contact_address);
    		cout << endl << "City                   : ";
    		getline(cin, contact_city);
    		cout << endl << "State (ABBREVIATE)     : ";
    		getline(cin, contact_state);
    		cout << endl << "Zip Code               : ";
    		getline(cin, contact_zip);
    		cout << endl << "Phone Number           : ";
    		getline(cin, contact_phone);
    		cout << endl;
    
    		system("cls");
    		f_i = contact_first.substr (0,1);
    		l_i = contact_last.substr (0,1);
    		full_name = contact_first + " " + contact_last;
    		full_address = contact_city + ", " + contact_state + " " + contact_zip;
    
    		cout << "*****************************************************" << endl;
    		cout << "***********"<<setw(15)<<f_i<<" "<<l_i<<setw(25)<<"************" << endl;
    		cout << "*****************************************************" << endl;
    		cout << "***********   " << endl;
    		cout << "***********   " << full_name << endl;
    		cout << "***********   " << contact_address << endl;
    		cout << "***********   " << full_address << endl;
    		cout << "***********   " << contact_phone << endl;
    		cout << "***********   " << endl;
    		cout << "*****************************************************" << endl << endl;
    		cout << "Do you want to enter another contact record? (Y/N): ";
    		cin >> run_again;
    		}
    		while (run_again == 'Y');
    		return(0);
    		break;
    
    	case '3':
    		cout << "TEST 3";
    		return(0);
    		break;
    
    	case '4':
    		cout << "TEST 4";
    		return(0);
    		break;
    
    	case '0':
    		return(0);
    		break;
    
    	default:
    		system("cls");
    		cout << "************************************************" << endl;
    		cout << "*****    I N V A L I D  R E S P O N S E    *****" << endl;
    		cout << "************************************************" << endl;
    		cout << '\n' << "Do you want to return to the main menu? (Y/N): ";
    		cin >> run_again;
    		break;
    	}
    	}
    while (run_again == 'Y');
    return (0);
    }
    For example, if the user executes option 2 (the contact card prog), is there a way to send the user back to the menu after the program is executed. I've been running into trouble because I already implement a do-while inside the case to repeat that. So I'm not sure how to return to the menu if the user selects not to repeat the case. Any insight is appreciated.

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by wco5002 View Post
    I already implement a do-while inside the case to repeat that.
    Instead of doing it inside the case try doing the entire switch statement in a while loop with one of the cases being the breaker for the while loop

    Code:
    while(!exit){
       cout<<"pick a number: "; cin>>option;
       switch(option){
          case 1:
             /*do something*/
          break;
    
          case 2:
             /*do something*/
          break;
    
          case 3:
             exit = true;
          break;
       }
    }

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    My only problem is that I want the user to have the ability to repeat the cases. Also, as you suggested, I've tried placing a while loop outside the switch but I've been getting a lot of errors, I just can't seem to figure out where to place it. Thanks for the help in advance.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Take out the return(0); statements in your cases.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    48
    wow, thank you, i hate when it's something stupid like that.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    I do too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM