Link to home
Start Free TrialLog in
Avatar of xanoo
xanoo

asked on

console menu selection

Hello

I'm trying to make a menu in c++ (dos/win32) console program that can take one or two arguments. As I understand a switch case would only take simple data structures.

First input would be a single char, while the second could be a letter, number or a name.
Based on the selection it runs the appropiate function, with name/number as argument if needed.

I would like a menu selection like this for example:

N             - new player data
P A          - display all players
P 23         - display player 23
P brian     - display data about brian
Q             - quit program

I've though about using a char array as input, and strtok somehow with alot of if else. But it will be a mess. Anyone got an example if they have done this before, or a better suggestion than strtok and if else's ?

thanks
- xan
Avatar of _corey_
_corey_

If you're trying to read in formatted text from the console, check out

scanf which will let you specify type and order of the variables such as integer and then string.

You can also use cin >> read from the console to variables of different types.
Avatar of Cayce
I suppose you're using C++Builder, since this is the C++Builder forum ;)

//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <System.hpp>
#include <SysUtils.hpp>
#pragma hdrstop
//---------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------
void Cleanup();
void DisplayAll();
void DisplayByNumber(int Number);
void DisplayByName(String Name);
void Create();
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
  AnsiString Input;
  Char Command = 0x00;
  do {
    string buffer;
    cout << "/------------------- MENU ------------------\\" << endl
         << "|  N           - new player data            |" << endl
         << "|  P A         - display all players        |" << endl
         << "|  P 23        - display player 23          |" << endl
         << "|  P brian     - display data about brian   |" << endl
         << "\\-------------------------------------------/" << endl
         << "Command:> " << endl;
    cin >> buffer;
    Input = buffer.c_str();
    Input = Input.Trim();
    Command = Input.SubString(1, 1)[1];
    String Parameters = "";
    if(Input.Length() > 1)
      Parameters = Input.SubString(2, Input.Length() - 1).Trim();

    switch(Command) {
    case 'Q':
      Cleanup();
    break;
    case 'P':
      if(Parameters.Length() == 1 && Parameters[1] == 'A')
        DisplayAll();
      else {
        try {
          int Number = Parameters.ToInt();
          DisplayByNumber(Number);
        } catch(EConvertError &e) {
          DisplayByName(Parameters);
        }
      }
      break;
    case 'N':
      Create();
      break;
    default:
      cout << endl << "Invalid Command" << endl;
      break;
    }
  } while(Command != 'Q');
  return 0;
}
//---------------------------------------------------------------------------
Hmm... forgot to check the size here

 Command = Input.SubString(1, 1)[1];

should be:

if(Input.Length() > 0)
   Command = Input.SubString(1, 1)[1];
else
   Command = 0x00;
Avatar of xanoo

ASKER

Sorry but I'm actually using visual studio for my project.
I had to (?) choose between c++ builder and mfc when I submitted, and since mfc is about windows programming I choose c++ builder. Correct me if I did something wrong.

These are not familiar to me in that case
#include <System.hpp>
#include <SysUtils.hpp>
#pragma hdrstop

Looks like I can use snippets of it anyways though, I'll give it a try.

thanks, xan.

Haha! Ok then. System.hpp and SysUtils.hpp are Borland C++ tools.

Just use the standard C++ strings for all the work. It's easier with AnsiString (the one I'm using there).

However the concepts are the same.

I'm building the same small app without AnsiStrings now
ASKER CERTIFIED SOLUTION
Avatar of Cayce
Cayce
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial