Thread: Help with a noob code

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Question Help with a noob code

    Ok me and my friends wanna make this simple program, to give us some expirence to make more, but to start we wanna make this one that disses one of our last names.
    can you tell me why when we enter coss it says thats a cool last name instead of he is a homo.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    
    {
        char string[50];
    
    printf("Enter your last name...\n");
    fgets(string, 50, stdin);
        if(string=="coss") {
            printf("you're homo\n");
        }
        else {
            printf("That's a cool last name\n");
        }
    
        return 0;
    }
    please help, I just started C a month ago so im not to skilled at all

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use strcmp function to compare strings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Question where

    where do we put strcmp in the code ?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    char a[] = "test";
    const char* b = "test2";
    if(strcmp(a,b) == 0)
    {
       puts("match");
    }
    else
    {
       puts("failed");
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    7

    Thumbs up thnks

    thank you much, ur were a big help

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    if(string=="coss") {
        printf("you're homo\n");
    }
    A little background on what's happening here and why this doesn't work...

    "coss" is what's called a string literal. The characters making up this are stored in the program's data segment somewhere at some specific address. The variable string is an array of characters that exist on the stack at some other specific address. In the above context, you basically asking "if the address of the variable string is equal to the address of the string-literal "coss" then print the message". If you think this through, the two objects here, the literal and the variable string, will never have the same address in memory, they are distinct objects. What you want to have happen here, and what the strcmp function does, is compare the values that are stored at those two different addresses and see if those contents are similar.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob question! Code completion - Array challenge
    By theleprechaun in forum C++ Programming
    Replies: 6
    Last Post: 08-22-2008, 08:58 PM
  2. Noob code question
    By jahkrohn in forum C Programming
    Replies: 6
    Last Post: 03-28-2008, 01:14 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM