-3
      #include <stdio.h>
      #include <stdlib.h>

what's wrong?

   int main()

this is the code

    {
    char planet;
    char earth;
    int  weight;
    float mass;
    int a=9;
    printf("Planet Name \n");
    scanf("%s",&planet);

the compiler does not reads the if statement it just jumps to the else statement

    if(planet=earth){

    printf("Enter your weight \n");
    scanf("%d",weight);
    mass=weight/a;
    printf("Your mass is %d",mass);}
    else {
    printf("Aww,snap!!!!!!We couldn't find the data");
    }

    return 0;}
Ayush Jha
  • 3
  • 1
  • 2
  • Possible duplicate of [String comparison in Objective-C](http://stackoverflow.com/questions/2592511/string-comparison-in-objective-c) – sinclair Jun 19 '16 at 10:14
  • i tried that too earlier but that doesn't work – Ayush Jha Jun 19 '16 at 10:14
  • My first comment was nonsens, please checkout the duplicate question. – sinclair Jun 19 '16 at 10:15
  • i tried that to earlier but it doesn't work – Ayush Jha Jun 19 '16 at 10:15
  • can you just type the code please @sinclair – Ayush Jha Jun 19 '16 at 10:17
  • 1
    @AyushJha How do you want this to work? `planet` is a single char, but you try to put a string into it, which is an issue waiting to happen. And you don't initialise `earth`, so who knows what it contains once the values would be compared (with `==`, not `=` if you're trying to compare single characters, but `strcmp` if you're trying to compare strings). – AntonH Jun 19 '16 at 10:27
  • 1
    There are so many bugs here it's hard to know where to start. Here are a few: (1) the `=` operator is assignment, not comparison (2) `earth` is undefined because you never assign to it, so it's value is unpredictable (3) You're only allocating single characters for `planet` and `earth`, so you have memory violations. You need to change them to arrays. (4) Your `scanf` call that's meant to set `weight` won't work because you aren't passing the address. Here's what you need to do: Study a C tutorial and learn the basics of the language, and turn on compiler warnings (and fix them). – Tom Karzes Jun 19 '16 at 10:34
  • 1
    You also need to use `strcmp` to compare strings, not `==`. – Tom Karzes Jun 19 '16 at 10:35
  • ok i am just a beginner i know very less about c and this is just my second project and by the way thanks for your help guys – Ayush Jha Jun 19 '16 at 10:40
  • You might also find the division `weight / a` does not give the result you want because it performs integer division. I suggest `mass = (float)weight / a;` – Weather Vane Jun 19 '16 at 10:43

3 Answers3

0

When you do this

if(planet=earth){

you are doing equivalent to:

char x = planet=earth;
if(x){

and not

bool x = planet==earth; if(x){

the wrong part is that according to the language spacification

if(x){

can be accepted by the compiler and in case x is not a boolean expression, it will be converted to a boolean one, BUT following the criteria:

  • false if x is zero, true otherwise.

so at the end

if(false){ //never met until the char given as input is a valid
    ...
else {
    printf("Aww,snap!!!!!!We couldn't find the data");
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You planet is a char so it can hold only one char but in scanf you are trying to read string instead. use %c instead of %s

scanf("%c",&planet);

Variable earth contains garbage initially because you haven't assigned any value to it. In the if statement you are assigning = instead of comparision ==.

When you are reading value for weight you are not using address ofoperator & hence it can't read the value.

mass=weight/a; here you should have used explicit typecasting else your result would be in int which would be later upgraded to float while assigning but your decimal value would already be lost by then.

int main()
 {
char planet;
char earth='A'; //at least assign something
int  weight;
float mass;
int a=9;
printf("Planet Name \n");
scanf("%c",&planet);

if(planet==earth){

printf("Enter your weight \n");
scanf("%d",&weight);
mass=(float)weight/a;
printf("Your mass is %d",mass);}
else {
printf("Aww,snap!!!!!!We couldn't find the data");
}

return 0;}

I didn't compile, should work.

ram619
  • 188
  • 1
  • 9
  • Change ```%d``` to ```%f``` in ```printf("Your mass is %f",mass);``` as it's float. Will work, just compiled – ram619 Jun 19 '16 at 17:30
0

The correct version of your code is this :

#include<stdio.h>
int main()
 {
char planet;
char earth='e';//Assign a value to this variable first.
int  weight;
float mass;
int a=9;
printf("Planet Name \n");
scanf("%c",&planet);

if(planet==earth){

printf("Enter your weight \n");
scanf("%d",&weight);
mass=(float)weight/a;
printf("Your mass is %f",mass);}
else {
printf("Aww,snap!!!!!!We couldn't find the data");
}

return 0;}

If you input 'e' in the console you will get your required answer after you enter the weight.

canopus7
  • 1
  • 4