Looong IF statement.. help

This is a discussion on Looong IF statement.. help within the C++ Programming forums, part of the General Programming Boards category; I have a very long IF Statement that doesn't seem to be working. What is the proper way to code ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    19

    Question Looong IF statement.. help

    I have a very long IF Statement that doesn't seem to be working. What is the proper way to code this:

    Code:
          if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' ||
          c == 'F' c == 'G' || c == 'H' || c == 'I' || c == 'X')


    Josh Stevanus
    jrstevan@svsu.edu

  2. #2
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    i dont know much about c++ my self, but shouldnt you be using double quotes?

    " value "

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    Code:
    if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' ||
          c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'X')
    only problem i see, without more code.

  4. #4
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Quote Originally Posted by alpha
    only problem i see, without more code.

    wow i missed that my self. good eye. Im pretty sure that would fix your problem.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you could try something like this:

    if( (c >= 'A' && c <= 'I') || c == 'X')

    but the logic gets pretty complex sometimes, so sometimes it's just easier to write out the entire list.

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I personally like to handle long IF statements by using the cascading property of switch/case:

    Code:
    char c;
    
    cout << "Enter Choice";
    cin >>  c;
    
         switch(toupper(c))
         {
              case 'A':
              case 'B':
              case 'C':
              case 'D':
              case 'E':
              case 'F':
              case 'G':
              case 'H':
              case 'I':
              case 'X':  do something;
    
              default :  else do something;
         }


    --Or you can try this--



    When you have a list of consecutive characters, you could just use a range of ascii values.. instead of looking for specific characters.

    I'm not totally positive on how to do this in c++.. so someone please back me up on this
    Code:
    //IF c is between the ascii value of A and below the ascii value of I   OR  c equals the ascii value of X
    
    if ((c >= 41h && c <=49h) || (c == 58h))
    Which I think can also be written like this:
    Code:
    if ((c>='A' && c<='I') || (c=='X'))
    which is much easier to write than all of this:
    Code:
    if (c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' ||
          c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'X')

    optimization == good


    [edit] I am slow.
    Last edited by The Brain; 12-08-2004 at 05:50 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by Frantic-
    i dont know much about c++ my self, but shouldnt you be using double quotes?

    " value "
    Double quotes are for string literals (zero or more characters), and single quotes are for character literals (one and only one character).

    The switch statement makes the most sense in this case, assuming the OP has learned it.
    Last edited by jlou; 12-08-2004 at 05:47 PM.

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    good catch.. just went back to edit my mistake
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 06:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 03:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 10:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM

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