Solved

console menu selection

Posted on 2004-04-16
6
1,247 Views
Last Modified: 2013-11-17
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
0
Comment
Question by:xanoo
  • 4
6 Comments
 
LVL 8

Expert Comment

by:_corey_
ID: 10845994
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.
0
 
LVL 9

Expert Comment

by:Cayce
ID: 10846565
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;
}
//---------------------------------------------------------------------------
0
 
LVL 9

Expert Comment

by:Cayce
ID: 10846581
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;
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:xanoo
ID: 10846659
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.

0
 
LVL 9

Expert Comment

by:Cayce
ID: 10846671
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
0
 
LVL 9

Accepted Solution

by:
Cayce earned 125 total points
ID: 10846860
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <sstream>
#pragma hdrstop
//---------------------------------------------------------------------------
using namespace std;
//---------------------------------------------------------------------------
void Cleanup() {}
void DisplayAll() {}
void DisplayByNumber(int Number) {}
void DisplayByName(string Name) {}
void Create() {}
//---------------------------------------------------------------------------
inline std::string trim(const std::string& s) {
  if(s.length() == 0)
    return s;
  int b = s.find_first_not_of(" \t");
  int e = s.find_last_not_of(" \t");
  if(b == -1) // No non-spaces
    return "";
  return std::string(s, b, e - b + 1);
}
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
  string       Input;
  char Command = 0x00;
  char         Buffer[80];

  do {
    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;

    Input.clear();
    cin.getline(Buffer, 80);
    Input = Buffer;

    Input = trim(Input);
    Command = Input.substr(0, 1)[0];
    string Parameters = "";
    if(Input.length() > 1)
      Parameters = trim(Input.substr(1, Input.length() - 1));

    switch(Command) {
    case 'Q':
      Cleanup();
    break;
    case 'P':
      if(Parameters.length() == 1 && Parameters[0] == 'A')
        DisplayAll();
      else {
        if(Parameters.length() >= 1) {
          int Number = atoi(Parameters.c_str());
          if(Number == 0 && Parameters.length() != 1 && Parameters[0] != '0')
            DisplayByName(Parameters);
          else
            DisplayByNumber(Number);
        }
        else
          cout << endl << "(P)rint what?" << endl;
      }
      break;
    case 'N':
      Create();
      break;
    default:
      cout << endl << "Invalid Command" << endl;
      break;
    }
  } while(Command != 'Q');
  return 0;
}
//---------------------------------------------------------------------------
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question