Thread: Program using menu selection and subprograms

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    12

    Program using menu selection and subprograms

    I know everything that i want to do but i never did a menu before so i dont know how to go about doing so if at all possible can someone post a sample program that uses a menu-selection with subprograms for each. This here is something like what i want my program to do


    I have a soft drink distrubutorship company that sells coca-cola(id number 1), pepsi(id number 2), sprite(id number 3), and root-beer(id number 4) by the case.

    The menu will do the following:

    (e) enter invetory
    (p) purchase sode
    (s) sold soda
    (d) display inventory
    (q) quit

    I will promt the user to enter the inventory for each brand at the start of the week and then process all weekly sales and purchase records for each brand.

    Each transaction will consist of 2 data items which are 1) brand identification number and 2) the amount purchased (a positive interger) or the amount sold (a negative integer). It is already assumed that i have sufficient foresight to prevent depletion of my inventory for all the brands. Also the the display of my weekly inventory will contain the brand's name.

    everything is inputted from the keyboard.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Menu selection is pretty simple; I'm sure there are tons of examples in the board archives (that are so cleverly hidden away...to get to them, you must use the well-disguised search button. However, I was bored. Here's one more example.
    Code:
    #include <iostream>
    #include <limits>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    int menu()
    {
    	char choice;
    	cout << "Here are a few things I might want to do:\n"
    		 << "1) Do my homework.\n"
    		 << "2) Post my homework on an Internet message board.\n"
    		 << "3) Throw away the homework and claim my dog ate it.\n"
    		 << "Choose any of the above, or enter 4 to quit." << endl;
    	cin >> choice;
    	cin.ignore(std::numeric_limits<int>::max(),'\n');
    	return choice;
    }
    
    void choice1()
    {
    	cout << "Good luck with your homework!" << endl;
    }
    
    void choice2()
    {
    	cout << "Poor choice; I hope that works out for you." << endl;
    }
    
    void choice3()
    {
    	cout << "Did that ever work?" << endl;
    }
    
    int main()
    {
    	char choice;
    	do
    	{
    		choice = menu();
    		switch (choice)
    		{
    		case '1':
    			choice1();
    			break;
    		case '2':
    			choice2();
    			break;
    		case '3':
    			choice3();
    			break;
    		case '4':
    			break;
    		default:
    			cout << "Try entering a number that's\
    				actually on the screen, bonehead." << endl;
    			break;
    		}
    	}
    	while(choice != '4');
    	return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    So you dont want to attempt it yourself. How do you think you would go about using a menu. After all it is a selection process and in your case involves characters...
    Code:
    cout << "Shall I answer this homework question for you, My choices are: \n";
    cout << "\nY for Yes\nN for No\nD for do it yourself\n\n"
    cin.get(answer);
    answer = toupper(answer);
    switch(answer)
    {
    case 'Y' : cout << "Okay";
                   break;
    case 'N': cout << "Not doing it";
                   break;
    case 'D': cout << "Go ahead";
                   break;
    default : cout << "Entered the wrong character";
                   break;
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

Popular pages Recent additions subscribe to a feed