Thread: is there a way to use a char variable in an if?

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    is there a way to use a char variable in an if?

    Hey, im pretty new and havent been able to get a good book yet so ive been using some tutorials i found on the internet.

    well my question is, is there a way to use
    a char variable in an "if". so far ive only learned about numbers.

    thanks.

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You can put anything you want into the expression, however, the value of the expression must ultimately be a type which can be implicitly converted to a value which can accurately represent 0 and other values (IE the end value of the expression can not be void, a user-defined struct, etc.).
    Last edited by Polymorphic OOP; 01-17-2003 at 06:00 PM.

  3. #3
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    of course.
    Code:
    char c;
    c = 'f';
    if( c == 's')
         cout << "S!";
    else if( c == 'f' )
         cout << "F!";
    else // etc.
         cout << "?";
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Yep..

    Is possible..
    Char holds numbers too, so you can do this too:

    Code:
    char chInput; //our char for input
    //print msg here and get an value for chInput
    
    if(chInput == 20)
       //stuff here
    else
      //stuff here

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    the example you gave me looks like its from c++
    can you give me an example in plane C?
    thanks
    ~matt~

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    The one post that uses C++ you would just change the cout<< statement to use the printf() function.

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ..

    A poor example, but this can help you:
    Code:
    #include <stdio.h>
    
    int main(void) {
      char chInput;
      
      while(chInput != 'E') { //while the user dont hit E
      printf("Enter a char: E for exit!\n");
      chInput = getchar();
      fflush(stdin);
      
      if((chInput == 'A') || (chInput == 'a'))
          printf("You did hit the A letter\n");
      else
         printf("You did hit a letter that its not A\n");
      }
      
      return 0;
    }

  8. #8
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    You compare a char with if like this:
    Code:
    char a = 'u';
    if ( a == 'u' )     //this will return true
    if ( a == 'o' )     //this will return false
    Don't forget to use strcmp to compare strings( array of char's ) with if.
    none...

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    28
    where excatly would i put the strcmp in the "if".

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    if( !strcmp( "SomeString", "SomeOtherString" ) )

    Where some string and some other string are the strings you are comparing (you'd usually put the names of your char arrays here).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  4. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  5. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM