Code problem....

This is a discussion on Code problem.... within the C++ Programming forums, part of the General Programming Boards category; Just wondering what people think about the code below. It could be improved with a few pointers, was this way ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Code problem....

    Just wondering what people think about the code below. It could be improved with a few pointers, was this way the best way to solve the problem? Is my coding sytle ok? Thanks in advace. The original question was:

    Write a program that asks the user to enter a character. The program will classify the character if it has any notable characteristics and it will output what those characteristics are. For example, entering a ‘w’ will output a message “w is a lower case character” but entering ‘W’ will produce the message “W is an upper case character”. If you enter a number (say 2) the message will be “2 is a number”. If you enter ‘a’ the message will be “a is a lower case letter and is a vowel” whereas ‘A’ will produce “A is an upper case character and a vowel”. If you enter a punctuation mark such as a . , “ ‘ : ; etc., the message will indicate that you have entered a punctuation character. E.g. if you enter ‘:’ the message is “: is a punctuation character”. If you enter an arithmetic operator such as ‘+’, the output will be “+ is an arithmetic operator”. If the character is any other non-characteristic value, output nothing.

    To enable your application to keep running and allowing you to continually enter characters, read up on the do while loop. In this case, the application will run until you press the escape key then return.
    The question does not ask to validate input, and was not coded to do so.

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    enum MessageIds {UPR_CHAR, LWR_CHAR, NUM, VOWEL_LWR, VOWEL_UPR, PUNCT, ARITH};
    
    void Messages(MessageIds, int, char);
    bool StringSearch(const char String[], char Temp, int &Pos);
    
    int main(void)
    {
      const int START_ALPHA_UPR = 65, END_ALPHA_UPR = 90, START_ALPHA_LWR = 97,
                END_ALPHA_LWR = 122, NUM_LWR = 48, NUM_UPR = 57,
                VOWEL_LWR_POS = 4, ESC = 27;
      const char VOWELS[] = "aeiouAEIOU\0", PUNCTUATION[] = ".,?';:!\0",
                 ARITHMETIC[] = "-+*/\0";
    
      MessageIds MsgIds;
    
      char Character;
      int AsciiVal, Index;
    
      do
      {
        clrscr();
        cout << "Please enter a character (Esc to quit): ";
        cin >> Character;
    
        AsciiVal = Character;
    
        if(StringSearch(VOWELS, AsciiVal, Index) && Index <= VOWEL_LWR_POS)
          Messages(MsgIds, VOWEL_LWR, AsciiVal); //needs to be first case
        else
        {
          if(Index != 10)
            Messages(MsgIds, VOWEL_UPR, AsciiVal);
          else
          {
            if(AsciiVal >= START_ALPHA_UPR && AsciiVal <= END_ALPHA_UPR)
              Messages(MsgIds, UPR_CHAR, AsciiVal);
            else
            {
              if(AsciiVal >= START_ALPHA_LWR && AsciiVal <= END_ALPHA_LWR)
                Messages(MsgIds, LWR_CHAR, AsciiVal);
              else
              {
                if(AsciiVal >= NUM_LWR && AsciiVal <= NUM_UPR)
                  Messages(MsgIds, NUM, AsciiVal);
                else
                {
                  if(StringSearch(PUNCTUATION, AsciiVal, Index))
                    Messages(MsgIds, PUNCT, AsciiVal);
                  else
                  {
                    if(StringSearch(ARITHMETIC, AsciiVal, Index))
                      Messages(MsgIds, ARITH, AsciiVal);
                  }
                }
              }
            }
          }
        }
    
      }while(AsciiVal != ESC);
    
      cout << "\nTada........";
      getchar();
    
      return 0;
    }
    
    void Messages(MessageIds MsgIds, int MsgCode, char Character)
    {
       switch(MsgCode)
      {
        case UPR_CHAR:
             cout << "\n" << Character << " is a upper case character.";
             getchar();
             break;
        case LWR_CHAR:
             cout << "\n" << Character << " is a lower case character.";
             getchar();
             break;
        case NUM:
             cout << "\n" << Character << " is a number.";
             getchar();
             break;
        case VOWEL_LWR:
             cout << "\n" << Character << " is a lower case letter and a vowel.";
             getchar();
             break;
        case VOWEL_UPR:
             cout << "\n" << Character << " is a upper case letter and a vowel.";
             getchar();
             break;
        case PUNCT:
             cout << "\n" << Character << " is a punctuation character.";
             getchar();
             break;
        case ARITH:
             cout << "\n" << Character << " is a arithmetic operator.";
             getchar();
             break;
      }
    }
    
    bool StringSearch(const char String[], char Temp, int &Pos)
    {
      bool MatchFound = false;
      int i = 0;
    
      while(String[i] != '\0' && !MatchFound)
      {
        if(String[i] == Temp)
        {
          MatchFound = true;
          Pos = i;
        }
        i++;
      }
      Pos = i;
      return MatchFound;
    }
    Be a leader and not a follower.

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I would opt for some inline functions like IsVowel() IsPunct(). There's always islower(), isupper() and isalpha() in the standard <ctype.h> or <cctype> that would do quite a bit of work for you.

    Asside from using the standard functions... it's just personal prefrence.

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    i'd say fix up that whole if-else mess
    hello, internet!

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Another thing... this looks a little weird:

    Code:
    if(Index != 10)
            Messages(MsgIds, VOWEL_UPR, AsciiVal);
          else
          {
            if(AsciiVal >= START_ALPHA_UPR && AsciiVal <= END_ALPHA_UPR)
              Messages(MsgIds, UPR_CHAR, AsciiVal);
            else
            {
              if(AsciiVal >= START_ALPHA_LWR && AsciiVal <= END_ALPHA_LWR)
                Messages(MsgIds, LWR_CHAR, AsciiVal);
              else
              {
                if(AsciiVal >= NUM_LWR && AsciiVal <= NUM_UPR)
                  Messages(MsgIds, NUM, AsciiVal);
                else
                {
                  if(StringSearch(PUNCTUATION, AsciiVal, Index))
                    Messages(MsgIds, PUNCT, AsciiVal);
                  else
                  {
                    if(StringSearch(ARITHMETIC, AsciiVal, Index))
                      Messages(MsgIds, ARITH, AsciiVal);
                  }
    Why not just use an if ... else if ... else statement?
    Edit:: Yeah, what moi beat me to

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Would you be able to elaborate a bit more on the if else problem, how could it be a bit more efficent?
    Be a leader and not a follower.

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Code:
    if (Index != 10) {
    	Messages(MsgIds, VOWEL_UPR, AsciiVal);
    
    } else if (AsciiVal >= START_ALPHA_UPR && AsciiVal <= END_ALPHA_UPR) {
    	Messages(MsgIds, UPR_CHAR, AsciiVal);
    
    } else if (AsciiVal >= START_ALPHA_LWR && AsciiVal <= END_ALPHA_LWR) {
    	Messages(MsgIds, LWR_CHAR, AsciiVal);
    
    } else if (AsciiVal >= NUM_LWR && AsciiVal <= NUM_UPR) {
    	Messages(MsgIds, NUM, AsciiVal);
    
    } else if (StringSearch(PUNCTUATION, AsciiVal, Index)) {
    	Messages(MsgIds, PUNCT, AsciiVal);
    
    } else if (StringSearch(ARITHMETIC, AsciiVal, Index)) {
    	Messages(MsgIds, ARITH, AsciiVal);
    
    }

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Right so the logic was ok, just the basic formatting of it.
    Thanks.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 02:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21