strcmp

This is a discussion on strcmp within the C Programming forums, part of the General Programming Boards category; hi everyone! i have a small problem. should be obvious by the error but im stumped. Code: void getname ( ...

  1. #1
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32

    strcmp

    hi everyone!
    i have a small problem. should be obvious by the error but im stumped.

    Code:
    void getname ( void )
    {
        char fname[BUFSIZ];
        char lname[BUFSIZ];
        int check;
        printf(" enter customers names type( quit ) to stop\n");
        while ( strcmp ( fname[check], "quit" ) != 0){
        printf( "customers first name:\n");
        fgets(fname, sizeof fname , stdin);
        printf( "customers last name:\n");
        fgets( lname, sizeof lname , stdin );
        check++;
    }
      
    }

  2. #2
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    edit: 80 C:\Dev-Cpp\names.c [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    strcmp (fname, "quit") you want to compare the full fname to quit

    [edit]
    also your going to want to remove the newline character from fname and lname after you read them in I assume
    Last edited by sl4nted; 12-04-2006 at 09:35 PM.

  4. #4
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    i tried
    Code:
    strcmp (fname, "quit") you want to compare the full fname to quit
    still dont work.
    is there a better way to do this?

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    did you remove the newline character from fname?

    and if it were me, I'd test fname and lname for quit after they were entered, since even if the user types quit for fname, you're still going to make them enter lname it looks like, which I doubt is what you want

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by tat
    edit: 80 C:\Dev-Cpp\names.c [Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast
    Did you #include <string.h>?
    Did you read this FAQ?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    tat
    tat is offline
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    why did the person that wrote fgets add a newline instead of a \0
    and why hasnt it been changed?
    just curious

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by tat
    why did the person that wrote fgets add a newline instead of a \0
    "He" didn't. "He" just left what was there to be interpreted by you.
    Quote Originally Posted by tat
    and why hasnt it been changed?
    Because C often assumes you know what you're doing.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    while ( strcmp ( fname[check], "quit" ) != 0)
    fname is not initialized when you make a first comparison...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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