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
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    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
    To be or not to be == true

  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
    23,773
    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.
    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

  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
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,754
    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
    To be or not to be == true

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