Thread: help comparing text values with if

  1. #1
    Counter-Strike Master
    Join Date
    Dec 2002
    Posts
    38

    Question help comparing text values with if

    im a n00b programer and im having some trouble comparing text strings.
    printf("\n\nWhat color is the sky?\n");
    scanf("%s",sky);

    if(sky=="blue")
    {
    printf("blew");
    }
    else
    {
    printf("%s",sky);
    }
    no matter what i type, it always skips 2 the else statement. i think that im doing the comparison wrong. also, can i make multiple comparisons in an if statement?

    if(sky=="blue", point=="up")

    help would be appreciated. thanks
    You say "Impressive!", but I already know it
    I'm a hardcore player and I'm not afraid to show it

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You should use the strcmp() function to compare strings and include string.h. Note that the function strcmp() returns 0 if the strings are equal.

    Code:
    if (strcmp (string1, string2) == 0)
    {
        // Strings are equal
    }
    Here's a nice reference for standard functions.

    http://www.f.kth.se/~f95-pax/refs/C%20Library%20Ref/

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953

    Re: help comparing text values with if

    Originally posted by Bigbio2002
    if(sky=="blue", point=="up")
    This is wrong you have to use operators like :&&, ||, etc...
    none...

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    19
    scanf("%s",sky);
    this should be
    Code:
    scanf("%s", &sky);
    cu
    wudmx

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by wudmx
    this should be
    Code:
    scanf("%s", &sky);
    cu
    wudmx
    No, actually sky by itself is just fine because an array name used alone represent a pointer to its first element. Taking the address of sky only will denote it as being a pointer to an array rather than a pointer to the first element.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing adjacent values in a file
    By typhonius in forum C Programming
    Replies: 10
    Last Post: 02-10-2009, 03:41 AM
  2. comparing return values from functions
    By bobthebullet990 in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2006, 03:16 PM
  3. question about comparing text in a file...
    By laar in forum C++ Programming
    Replies: 12
    Last Post: 03-19-2006, 02:03 PM
  4. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM