Thread: Menu Selections in a command prompt

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    Menu Selections in a command prompt

    I'm looking for an alternative solution to this mess:
    Code:
    int main()
    {
    	int selection;
    	string input;
    	bool goodinput = false;
    	bool programexit = false;
    
    	cout << "Welcome to the employee database, what would you like to do?" << endl		// prompt
    			<< "1. Add an employee" << endl << "2. Modify an employee" << endl
    			<< "3. Exit the program" << endl;
    	while (programexit == false)
    	{	
    		goodinput = false;
    		while (goodinput == false)
    		{
    			getline(cin, input);	//get input
    			stringstream(input) >> selection;	//extract selection integer
    			if (selection == 1)
    			{
    				goodinput = true;
    				employees.push_back(create());
    			}
    			else if (selection == 2)
    			{
    				cout << "Enter the name of the employee you'd like to edit: ";
    				getline(cin, input);
    				get_employee(input).edit();
    				goodinput = true;
    			}
    			else if (selection == 3)
    			{
    				programexit = true;
    				goodinput = true;
    			}
    			else
    			{
    				goodinput = false;	// invalid selection - continue loop
    				cout << "Please enter a valid selection, 1 or 2." << endl;
    			}
    		}
    	}
        return 0;
    }
    This works, but it's very limited. Like when I run the create function or edit function, or anything inside the main loop, I have to re-initialize my programexit bool. I also have to reinitialize my input variable, as well as the goodinput bool. So there is 3 local variables that I'm always going to need, no matter what user interaction function I would run. Is there any relatively simple solution to programming context menus? I know I'll have to manually enter in logic for specific commands, but I'm wondering is there a more object oriented approach to dealing with these context menu's? I'm open to a good tutorial on the subject, or some tips if you have any. Thank you!
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Maybe I could use a class, which has generic responses, like cancel, exit, or what have you.

    And use overloaded operators to add choices, Hmmmm.... The wheels are turning.

    Perhaps I would need a template menu with references to functions as arguments?

    I've never really tried anything like this before. Oh how I love programming!
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I want something like this:

    interface.generate_menu()
    Menu.add_context(object.get_context())

    Menu.display_content();
    Menu.get_input();
    Menu.execute_selection();

    Ultimately: interface.update();
    Last edited by Shamino; 01-23-2012 at 06:47 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Maybe
    Code:
    int main()
    {
    	int selection;
    	string input;
    	bool done = false;
    
    	cout << "Welcome to the employee database, what would you like to do?" << endl		// prompt
    			<< "1. Add an employee" << endl << "2. Modify an employee" << endl
    			<< "3. Exit the program" << endl;
    	while ( !done )
    	{	
    			getline(cin, input);	//get input
    			stringstream(input) >> selection;	//extract selection integer
    			if (selection == 1)
    			{
    				employees.push_back(create());
    			}
    			else if (selection == 2)
    			{
    				cout << "Enter the name of the employee you'd like to edit: ";
    				getline(cin, input);
    				get_employee(input).edit();
    			}
    			else if (selection == 3)
    			{
    				done = true;
    			}
    			else
    			{
    				cout << "Please enter a valid selection, 1 or 2." << endl;
    			}
    		}
    	}
        return 0;
    }
    Good input is really determined by something like this, not by the user typing in another number.
    Code:
    if ( getline(cin, input) && stringstream(input) >> selection ) {
      // do your thing
    } else {
      // either cin failed (say eof), or what was typed wasn't an integer)
      // if cin has failed, then set done to true
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Code:
    #include <vector>
    #include <string>
    #include <sstream>
    #include <iostream>
    using namespace std;
    
    class Menu
    {
    public:
    	void display_options()
    	{
    		for (unsigned int i = 0; i < menu_options.size(); i++)
    		{
    			cout << i+1 << ". " << menu_options[i] << endl;
    		}
    	}
    
    	int execute_selection(string input)
    	{
    		unsigned int selection;
    		if (stringstream(input) >> selection)	//extract selection integer
    		{
    			if ( selection-1 > menu_options.size())
    			{
    				return -1;
    			}
    			else if (selection-1 < menu_options.size())
    			{
    				return selection-1;
    			}
    		}
    		else
    		{
    			return -1; // return -1 for invalid input
    		}
    	}
    private:
    	vector<string> menu_options;
    };
    Okay, this is going to be 1 component of my interface. I still need to make a proper constructor. I'm going to use the interface class to retrieve information about an object, and construct a context menu based off of that object's information.

    For instance employee will tell interface, hey there are two functions that I'll allow you to manipulate, just send me an integer back corresponding to which function you want to operate.

    So interface will construct a menu object based off of what employee tells it. Interface will then get input from the user, tell the menu what the user inputs, the menu will check against valid options, and depending on the logic above, it will return an integer corresponding with the valid menu option.

    Interface will then use the information it got back from the menu, if valid, it will tell the object to execute one of it's functions based on the information given.

    It sounds like a lot, but I think it will be more re-usable in the long run if I can get it working.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Okay, so right now, menu handles the object itself, but interface needs to remember where it is in the program.

    For instance, begin the program with a context menu which allows us to get information about the vector of employee objects.

    Then we choose a selection that wants to get access to a specific employee object, and we enter a new context menu based off of that employee object.

    Interface needs to remember where it was and how to get back there.

    I.E -> it needs to keep track of menu objects!

    Fwehhh, this is quite a complicated bit of programming... Because by no means is it employee's responsibility to keep track of the last object interfaced with.

    I think the menu class needs some flags. Perhaps an isLast flag. (I'm trying to do this without pointers) Then I can have interface adjust the last menu as needed.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command Prompt
    By Trafalgar Law in forum Tech Board
    Replies: 3
    Last Post: 10-09-2008, 06:00 PM
  2. command prompt++
    By l2u in forum Tech Board
    Replies: 13
    Last Post: 04-29-2007, 12:21 AM
  3. Command Prompt here
    By Salem in forum Tech Board
    Replies: 0
    Last Post: 04-11-2005, 11:10 AM
  4. Help with menu and integer selections
    By EliasK in forum C Programming
    Replies: 2
    Last Post: 04-03-2003, 09:35 AM
  5. No command prompt please!
    By Lurker in forum Windows Programming
    Replies: 3
    Last Post: 03-07-2003, 04:00 PM