Thread: problem with letters

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    34

    problem with letters

    I have this code but it seems no matter what letter I choose it goes to if and not else:
    Code:
    void consonant()
    {
    	do
    	{
    		printf("Pick a letter: ");
    			letter=getch();
    		if (letter == 'a', 'e', 'i', 'o', 'u')
    			printf("You can't pick a vowel choose again!!\n");
    		else
    			printf("\nYou choosed %c.\n\n", letter);
    			valid = 1;
    	}
    	while(valid == 0);
    	
    	endturn = 0;
    	winner = 0;
    }
    Thanks for the help

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    if (letter == 'a', 'e', 'i', 'o', 'u')
    I don't think that's legal. You either have to say:
    Code:
    if (letter == 'a' || letter == 'e' || ...)
    or you can write a macro or a function to do that for you:
    Code:
    #define is_vowel(x) ( x == 'a' || x == 'e' || x == 'i' ...)
    
    if (is_vowel(letter)) ...

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    9
    Originally posted by Salem
    > I don't think that's legal. You either have to say:
    Oh its quite legal - its just pretty useless - and almost certainly not what was wanted.
    It basically degenerates into if ( 'u' != 0 )
    Why exactly would it degenerate into that? I don't see why thatis the case, although I can see that the initial statement was wrong.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Why exactly would it degenerate into that?

    From C99:
    The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation. Then the right operand is evaluated; the result has its type and value.
    So, first letter == 'a' is first evaluated as a void expression, then 'e' is evaluated as a void expression, then 'i' is evaluated as a void expression, then 'o' is evaluated as a void expression, then 'u' is evaluated. This is the condition in the if statement, whether 'u' is nonzero. (And that would be a pretty safe bet!)
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. How is it possible to solve this problem
    By ahmet858585 in forum C Programming
    Replies: 7
    Last Post: 05-01-2007, 12:28 PM
  3. strcmp, a problem with capital letters
    By RichardH in forum C Programming
    Replies: 11
    Last Post: 04-23-2007, 08:49 AM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM