Thread: Menu Creation Question(s)

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    41

    Menu Creation Question(s)

    I am in the process of creating a series of menus for a program I am writing. I have the Menu_Opt field defined as:
    Code:
     Char Menu_Opt[4];
    This way I can support a three character option and the necessary null end character (I believe is what they're called). I would like to be able to validate that the first character is an upper case letter and if it is not, I want to convert it to upper case. Then, I want to make sure that the two digit portion is within a certain number range. This is how I would like to validate that it is a valid option.
    My question is, should I create a structure to support the menu option, or use a substring function to extract each part, or do you have other suggestions? Either way, may ask for your advice and how I can do this?

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    It is only 3 characters...
    Code:
    Menu_Opt[0] = toupper(Menu_Opt[0]);
    // Within [10..60)
    int value = atoi(Menu_Opt + 1);
    if (value < 10 || value > 59)
      error();
    You can put the validation in a function for simpler code. strtol is better than atoi, but is harder to understand. strings and stringstreams are best, you should use them instead of array strings.

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Didn't I do exactly what you're asking in the changes I made to your program? Well as a matter of fact yes, yes I did:
    Code:
      char type = std::toupper(str[0]);
        
      if (type == 'Q')
        return;
      else if (type == 'A') {
        // ...
        return;
      }
      else if (type == 'Y') {
        int newCurr = std::atoi(str.substr(1).c_str());
        if (newCurr < 0 || newCurr >= NUM_CURR)
          return;
        _curr = (Currency)(newCurr - 1);
      }
    So the variable "type" contains the first character of the input. Use isalpha(type) to check that it is an alphabetic character. And the check to insure the numeric part is within a certain range is already included.

    Yours is such a limited and specific use that creating a class/struct would be overkill.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Simple Menu Questions
    By Olidivera in forum Windows Programming
    Replies: 4
    Last Post: 06-03-2006, 05:29 PM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM