1

So, I'm just starting out (excuse my noob-ness), and I'm working on a C program on a Mac with Xcode. It's basically just a scanf() and then a ton of if statements that produce a pre-determined output. I've wrote it so that Xcode compiles it without bitching, but I'm getting a strange "Fix-it" error and no output when I try to run it.

char planet[9];

printf("Input the name of a planet\n");

 scanf("%c", &planet);

if (planet == "Earth")
    {printf("Earth is 150 million kilometers away from the sun");}

if (planet == "Mars") 
    {printf("Mars is 220 million kilometers away from the sun");} 

if (planet == ("Mercury"))
{printf("Mercury is 57 million kilometers from the sun");}

if (planet == ("Venus"))
    {printf("Venus is 108 million kilometers from the sun");}

if (planet == ("Jupiter"))
    {printf("Jupiter is 779 million kilometers from the sun");}

if (planet == ("Saturn"))
    {printf("Saturn is 1.73 billion kilometers from the sun");}

if (planet == ("Uranus"))
    {printf ("Uranus (haha) is 2.88 billion kilometers from the sun");} 

if (planet == ("Neptune"))
    {printf("Neptune is 4.5 billion kilometers from the sun");}

    return 0;

is the code itself but I can't get it to work.

Here's a link to the Xcode project as well.

https://www.facebook.com/photo.php?fbid=252616984794310&set=a.106237419432268.11635.100001380326478&type=1&theater

  • What happens when you use `scanf("%s", &planet);`? Oh, you could have mentioned the planet `Klingon` in there too. – Michael Dautermann Nov 26 '11 at 05:45
  • possible duplicate of [How to compare strings in an "if" statement?](http://stackoverflow.com/questions/8222495/how-to-compare-strings-in-an-if-statement) This one comes out first if I search on SO for "[C] compare string". Please use the search capabilities of SO before asking. – Jens Gustedt Nov 26 '11 at 08:50

3 Answers3

4

The address of planet will never be equal to the address of any of those string literals. You need to use strcmp to compare the contents of strings rather than comparing their addresses.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
4

You'd be much happier using strcmp:

if (strcmp(planet, "Earth") == 0) {
    ...
}

Also, %c scans one character, not a string. You need to use %s to scan a string. And you need to specify a maximum length to avoid overflowing your buffer:

scanf("%8s", planet);

The maximum length is one less than the buffer size because you have to leave room for the NUL terminator.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
0

Your are comparing addresses of planet to the address of those string literals..so the addresses will not be same. You should instead compare content of both strings so do as follows:

First include <string.h> and in each if statement write like this

if(!strcmp ( planet,"Earth" ))

As c strings can not be compared directly you need to use strcmp function for that.

strcmp function

int strcmp ( const char * str1, const char * str2 );

this function compare two strings and return value will be :

a) A zero value indicates that both strings are equal.

b) A value greater than zero indicates that the first character that does not match has a greater value in str1 than in str2; And

c) a value less than zero indicates the opposite.

antzshrek
  • 9,276
  • 5
  • 26
  • 43
Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222