Thread: Newbie question about if()

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    10

    Newbie question about if()

    I'm working through the C tutorial (if statements) and I want to use an if() statement to check the user input and act on it accordingly.

    It seems that I always hit the final 'else' statement even though the character stored in the variable is correct (namely A or S in my case). Is there something odd about evaluating char variables as opposed to int etc?

    Any pointers would be helpful. Code attached for reference.

    Code:
    void askuser_fn()
    {
       /* Define our variables here */
       char user_input[1];
    
       clear_screen
    
       /* Ask the user what to do */
       printf( "Would you like to add or subtract?\n\n" );
       printf( "Enter A for Add and S for Subtract: " );
       scanf ( "%s", &user_input );
    
       /* Let's see what it is that the user wants us to do */
       if ( user_input == "A" ) {
          addition_fn();
       }
          else if ( user_input == "S" ) {
             subtraction_fn();
          }
             else printf( "Sorry you entered an invalid option. Option entered was: %s\n", &user_input );
    }

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    "A" is a string, and you can't compare strings with == (the address of the string literal will actually be compared here). Use 'A' (single quotes) if you want to compare against the character A. A char array of size 1 is quite redundant as well. :P

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    That's because you are trying to do a string comparison on a character field. Try using single quotes, not double quotes. Also, user_input is a string array. Try using user_input[0] or *user_input.
    Code:
     /* Let's see what it is that the user wants us to do */
       if ( user_input[0] == 'A' ) {
          addition_fn();
       }
          else if ( user_input[0] == 'S' ) {
             subtraction_fn();
          }
             else printf( "Sorry you entered an invalid option. Option entered was: %s\n", &user_input );
    }

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    Thanks for the replies. I'll go try it out.

    With regards to the string array etc - this is due to my getting ahead of myself as I haven't learnt them yet. Thanks for the tips though.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
       scanf ( "%s", &user_input );
    And you dont' need & here.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    Okay so I got it working and I'm adding some more "checking".

    I found that this works:
    Code:
       if ( ( user_input[0] == 'A' ) || ( user_input[0] == 'a' ) ) {
    However if I try this:
    Code:
       if ( user_input[0] == 'A'  || 'a'  ) {
    Then it doesn't in a sense. I find that no matter what I enter, this first IF entry is always evaluated as TRUE, even if I type 'X' into the input. Perhaps I need to brush up on my boolean logic.

    @Bayint Naung
    Thanks for the tip.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    What is being evaluated as true is not the first part, but the second part.

    Code:
    if ( user_input[0] == 'A' || 'a' ) {
    'a' will always be true, so when you say || 'a' you are always making a true statement.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    It's called operator precedence.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    Quote Originally Posted by samf View Post
    'a' will always be true, so when you say || 'a' you are always making a true statement.
    I still don't understand why - but as I'm still a noob, I'll use the first example of code and get back to the tutorials.

    Thanks for helping nonetheless.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    58

  11. #11
    Registered User
    Join Date
    Jun 2010
    Posts
    10
    Yep that's what I'm working through - but it would seem that I've missed a trick.

    The penny will drop at some point! :-)

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Code:
    if ( user_input[0] == 'A' || 'a' ) {
    This is not doing what you think it is. It is not checking that char_input[0] is A or if it is a, it is checking first if char_input[0] is A, then it is checking if 'a' is true. Since 'a' is nonzero in ASCII and any nonzero value is true, then it always evaluates to true.

    You have to explicitly write out each check you want to perform, as you did in the first bit of code you said was working.

    The above code is equivelent to:

    Code:
     
    if ( user_input[0] == 'A')
    {
          ...do something...
    }
    else if ('a')
    {
          ...do the same thing...
    }
    Last edited by KBriggs; 06-21-2010 at 10:38 AM.

  13. #13
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    There's a few things wrong with that program.

    Code:
    askuser_fn()
    {
       /* No need to specify an array size if it's only a single character. */
       char user_input; 
       clear_screen; 
       /* You terminate statements with a semicolon, silly. */
    
       printf("Would you like to add or subtract?\n\n");
       printf("(A)dd (S)ubtract: ");
       /* That's how the classics did it. */
       scanf("%c", &user_input);
       /* Since the program expects a single character, use %c instead of %s. */
    
       if (user_input == "A" || user_input == "a")
             addition_fn();
       /* Don't use braces if it's only a single statement. Just indent. */
       
       if (user_input == "S" || user_input == "s")
             subtraction_fn();
    
       else 
             printf("Sorry you entered an invalid option. Option entered was: %c\n", user_input);
    }

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to use single quotes instead of double quotes.
    FYI, to compare strings, you need to use strcmp. You should be able to look up that function in any manual. Good training.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 06:24 AM
  2. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  3. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  4. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM