Problem Using Strings in IF statements

This is a discussion on Problem Using Strings in IF statements within the C Programming forums, part of the General Programming Boards category; When I run the code, put it 'yes', the code does not print 'Hi'. How can this be fixed? Code: ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    Problem Using Strings in IF statements

    When I run the code, put it 'yes', the code does not print 'Hi'. How can this be fixed?

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    #include <ctype.h> 
    
    char command[100];
    
    
    int main(void)
    {
    printf("Please Enter 'yes' or 'no' :: ");
    scanf("%s",&command);
    
    if(command == "yes")
    {
    printf("Hi");
    }
    
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. do not use glabals
    2. indent your code
    3. do not use & when passing string to scanf
    4. modify your format - add width specifier to prevent buffer overrun
    5. use strcmp() to compare strings
    6. add return 0; to the end of main
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    You are comparing the first elements memory address to the character constant "yes". You have the right idea that scanf requires its arguments to be passed by reference, however an array already is a memory address, so dont add the &. Other then that use strcmp() for comparing strings like vart said, and your program will work.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    an array already is a memory address
    That is not accurate: an array decays to a pointer to its first element when passed as an argument, but arrays are not memory addresses.
    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

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by laserlight View Post
    That is not accurate: an array decays to a pointer to its first element when passed as an argument, but arrays are not memory addresses.
    Before anyone tries to counter what laserlight is saying, yes, an array's address and the address of its first element may be the same, and for many intents and purposes you can use arrays and pointers interchangably, however, they are NOT the exact same thing as far as C is concerned.

    In memory, addresses are addresses, but C takes the type of the data into account when performing manipulations on addresses, and in C an array is a type, distinct from a pointer.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    An array is a constant pointer to a type right? Or wrong? >< I know that at compile time arrays are transformed into pointer form, but is it safe to think of an array as a constant pointer or just as a totally seperate data type?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by valaris View Post
    An array is a constant pointer to a type right? Or wrong? >< I know that at compile time arrays are transformed into pointer form, but is it safe to think of an array as a constant pointer or just as a totally seperate data type?
    arrays are casted to pointers when appropriate...

    But if you apply sizeof operator for example - you'll get the totally different output for array and pointer
    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. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  3. Delphi DLL Problem, C++ hates Delphi strings
    By Cogman in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2008, 07:32 PM
  4. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 10:47 PM
  5. Newbie having problem with strings
    By sunzoner in forum C++ Programming
    Replies: 3
    Last Post: 05-21-2002, 08:34 PM

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