Help: strcmp

This is a discussion on Help: strcmp within the C Programming forums, part of the General Programming Boards category; Ok lets say name is a char and I typed Bruce what value would I get? strcmp( name ,"Bruce") 1 ...

  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,796
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    20,975
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05: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, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21