wheres my error

This is a discussion on wheres my error within the C Programming forums, part of the General Programming Boards category; Where am i going wrong? printf executes with any input. Code: int main( ) { char name[10]; scanf ("%s", name); ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    15

    wheres my error

    Where am i going wrong?
    printf executes with any input.

    Code:
    int main( )
    {
    char name[10];
     scanf ("%s", name);
     
    if("name==bob")printf ("nice name" );
     
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "name==bob" is a string. That string exists, therefore it is true, therefore the true part of the if statement happens.

    One, if you wanted to do == you wouldn't put the whole thing in quotes, and two, you can't use == to compare strings anyway (instead: strcmp).

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    CoCo,

    Use strcmp() for that.
    Code:
    if(!strcmp(name, "bob")) printf(...);

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    That Great. Can I use strcmp to compare multiple strings

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by CoCo View Post
    That Great. Can I use strcmp to compare multiple strings
    Look it up in your documentation... how many parameters does it accept?

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    15
    There are 5 different names which when entered must display different messages

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Then you're probably going to need 5 different calls to the strcpy function to account for each case.
    "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. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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