Thread: Checking Multiple Values in if() statement

  1. #1
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19

    Checking Multiple Values in if() statement

    hey guys i keep going off and on with my programming resulting in my forgeting how to do most everything

    ok so the program im working on is supposed to display all the ascii characters and their value I do not want to show the values that do not contain an ascii character (7,8,9,10,13,32) and i remember how to check the index for multiple values in a switch() but i think i remember doing it all in an if() statement but i cant remember what the correct syntax is for that.

    i was trying this
    Code:
    if(1 == 7 || 8 || 9 || 10 || 13 || 32) i++
    anyways ill post my code and if you can help me out that would be great Thanks

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        cout<<"Here we go!\n";
        char var1 = 55;
        cout<<"\nvar1 is "<<var1<<endl;
        cout<<"the size of Var1 is: "<<sizeof(var1);
        for(unsigned char i =0; i<255; i++)
        {
              switch(i)  //how to check i for multiple values in if statement
              {
                  case 7: i++;
                  case 8: i++;
                  case 9: i++;
                  case 10: i++;
                  case 13: i++;
                  case 32: i++;
              }
              printf("%d %c",i, i); // How to display same results in c++ !c
              cout<<endl;
              }
        system("pause");
    }//end main
    i also have another question, How would i get the same result the printf() statement has using cout?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Zero_X
    i was trying this
    Code:
    if(1 == 7 || 8 || 9 || 10 || 13 || 32) i++
    Try this:
    Code:
    if(1 == 7 || i == 8 || i == 9 || i == 10 || i == 13 || i == 32) i++
    or this
    Code:
              switch(i)  //how to check i for multiple values in if statement
              {
                  case 7: case 8: case 9: case 10: case 13: case 32:
                     i++;
              }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    awesome, i didnt know i could stack the case's like that. but now ive run into another problem im not sure if i will solve. for some reason im still getting 8 && 10 to show with no ascii character. i really have no idea what is going on with this.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Yeah, you've chosen to simplistically "increment past" the offending value. What's wrong with this logic?

    You actually don't want to print the value, but to instead proceed with the loop to the next character, right? "Word it" so offending values don't print.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User Zero_X's Avatar
    Join Date
    Mar 2006
    Posts
    19
    ahh yes I see now

    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        cout<<"Here we go!\n";
        char var1 = 55;
        cout<<"\nvar1 is "<<var1<<endl;
        cout<<"the size of Var1 is: "<<sizeof(var1);
        for(unsigned char i =0; i<255; i++)
        {
              if(i == 7 || i == 8 || i == 9 || i == 10 || i == 13 || i == 32)i++;
              else{printf("%d %c",i, i); // How to display same results in c++ !c
              cout<<endl;}
              }
        system("pause");
    }//end main
    Thanks for the help Dave

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It may be easier to see your question if it isn't in the code.
    Code:
       for ( unsigned char i =0; i<255; i++ )
       {
          if ( i == 7 || i == 8 || i == 9 || i == 10 || i == 13 || i == 32 )
          {
             /* skip */
          }
          else
          {
             // How to display same results in c++ !c
             cout << static_cast<int>(i) << ' ' << i << '\n';
          }
       }
    Or is otherwise perhaps highlighted.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45
    I'm curious why you output past the 127th character.

    Further, you could use <ctype.h> for a simpler implementation.

    -Cheers
    Last edited by CaptainMorgan; 10-02-2006 at 11:29 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning Multiple Values to Array/ Vector
    By bengreenwood in forum C++ Programming
    Replies: 2
    Last Post: 07-02-2009, 01:15 PM
  2. get multiple values from INI file
    By hiya in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2005, 03:00 PM
  3. Writing multiple values to an array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-06-2002, 03:27 PM
  4. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 10:09 AM
  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