Solved

console menu selection

Posted on 2004-04-16
6
1,243 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_
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:xanoo
Comment Utility
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
Comment Utility
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
Comment Utility
//---------------------------------------------------------------------------
#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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now