Thread: Help: strcmp

  1. #1
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20

    Help: strcmp

    Ok lets say

    name is a char and I typed Bruce

    what value would I get?

    strcmp(name,"Bruce")

    1 or 0?

    and if it is one explain why this code source doesn't work

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {     char name[21];
          int  age;
          
        puts("Are you the chosen one?");
        printf("Please ENTER your first name: ");
        gets(name);
        printf("Please enter your age: ");
        scanf("%d", &age);
        
        if (age >= 18 && strcmp(name,"Bruce") == 1)
        printf("You are the CHOSEN ONE.\n");
        else
            puts("Sorry your not the chose one.");
        
        fflush(stdin);
        getchar();
        
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    strcmp returns 0 if the two strings are equal.
    My best code is written with the delete key.

  3. #3
    Registered User LuizCPFL's Avatar
    Join Date
    Dec 2008
    Location
    Louisiana
    Posts
    20
    thanks

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    And using gets() and fflush(stdin) will get you something else as well.
    Read the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Just remember it like this:
    Code:
    if(strcmp(name, "AnswerKey") == 0){
    TRUE MATCH
    }
    
    if(strcmp(name, "ANSWER")){
     FALSE MATCH
    }
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would recommend the latter as:
    Code:
    if(strcmp(name, "ANSWER") != 0){
     /* FALSE MATCH */
    }
    since it reminds the reader that strcmp() does not return a boolean value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM