Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

console menu selection

Posted on 2004-04-16
6
Medium Priority
?
1,252 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

916 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