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.
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.
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.
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.)
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
the example you gave me looks like its from c++
can you give me an example in plane C?
thanks
~matt~
The one post that uses C++ you would just change the cout<< statement to use the printf() function.
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; }
You compare a char with if like this:
Don't forget to use strcmp to compare strings( array of char's ) with if.Code:char a = 'u'; if ( a == 'u' ) //this will return true if ( a == 'o' ) //this will return false
none...
where excatly would i put the strcmp in the "if".
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).