Solved

console menu selection

Posted on 2004-04-16
6
1,246 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to edit a midi track 9 166
How to navigate in javascript code from Intellij Idea 2 52
missing "main" in Java on Eclipse 4 70
The line on IDE 4 84
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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

776 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