Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

My code is returning 0 and I dont know why

AdamBGames
 Share

Hi, So I need to get this code working for tommorow but I have no idea what is wrong, Im simply trying to input 2 variables, use a simple calculation then output that. but for some reason my code says it is getting 0. Note that i am writing this is C, not C++575d1f8c07123c466edc3855a9996d94.png
https://gyazo.com/575d1f8c07123c466edc3855a9996d94

Main PC | AMD R7 3700X | Noctua D14 | MSI RTX 2080 Super XS OC | Corsair Vengence LPX 32GB DDR4 3200MHz | 1TB PNY XLR8 NVMe SSD | ASUS TUF Gaming B550 Plus | 2 x 1TB Seagate Barracuda HDD | Kingston A400 960GB SSD | 4TB Seagate Barracuda HDD | Fractal Design Define R6TG |

Laptop (Asus TUF FX505DY) | R5 3550H | RX560X | Crucial DDR4 16GB 2400MHz | Western Digital SN550 256GB SSD | PNY CS900 960GB SSD |

Phone | Samsung S10 Lite (128GB + 128GB SD card) |

Other Cool Stuff | Steam Link | Sontronics Podcast Pro | NZXT Hue+ | Corsair K70 MK 2 (MX Brown) | Logitech G402 | HiSense A7300 43 Inch 4K TV | Logitech C920 | Ender 3 Pro with Bulleye Fan duct and BLTouch |Sony PS4 | Nintendo Switch 

Link to comment
Share on other sites

Link to post
Share on other sites

Unsure about C, but have you checked whether console input (text characters) can be assigned to a double as you may be entering a string into the console.

 

Also, you can change 'height*height' on line 18 to 'height^2', I believe.

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, MrJeeves said:

Unsure about C, but have you checked whether console input (text characters) can be assigned to a double as you may be entering a string into the console.

 

Also, you can change 'height*height' on line 18 to 'height^2', I believe.

 

Ive checked and it is inputting a double. thanks for the power thing though as that seems to work in my other projects but not this

Main PC | AMD R7 3700X | Noctua D14 | MSI RTX 2080 Super XS OC | Corsair Vengence LPX 32GB DDR4 3200MHz | 1TB PNY XLR8 NVMe SSD | ASUS TUF Gaming B550 Plus | 2 x 1TB Seagate Barracuda HDD | Kingston A400 960GB SSD | 4TB Seagate Barracuda HDD | Fractal Design Define R6TG |

Laptop (Asus TUF FX505DY) | R5 3550H | RX560X | Crucial DDR4 16GB 2400MHz | Western Digital SN550 256GB SSD | PNY CS900 960GB SSD |

Phone | Samsung S10 Lite (128GB + 128GB SD card) |

Other Cool Stuff | Steam Link | Sontronics Podcast Pro | NZXT Hue+ | Corsair K70 MK 2 (MX Brown) | Logitech G402 | HiSense A7300 43 Inch 4K TV | Logitech C920 | Ender 3 Pro with Bulleye Fan duct and BLTouch |Sony PS4 | Nintendo Switch 

Link to comment
Share on other sites

Link to post
Share on other sites

The calculation it runs return something like 0.0175, so it might be possible that your code just doesn't want to display these amounts of decimals. 

Try entering 4 and 2 for weight and height respectively (which should return 1.0) and see if it displays that or still 0.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

I think the issue is that %f in scanf() means to read a float, but you're assigning it to a double.

 

Try using %lf instead, like in the code below.

 

int main()
{
    double weight;
    double height;
    float bmi;
    
    printf("Enter weight (kg): ");
    scanf("%lf", &weight);
    
    printf("Enter height (cm): ");
    scanf("%lf", &height);
    
    height = height / 100;
    
    bmi = weight / (height * height);

    printf("Your BMI is %f\n", &bmi);
    
    return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

37 minutes ago, MrJeeves said:

I think the issue is that %f in scanf() means to read a float, but you're assigning it to a double.

 

Try using %lf instead, like in the code below.

 


int main()
{
    double weight;
    double height;
    float bmi;
    
    printf("Enter weight (kg): ");
    scanf("%lf", &weight);
    
    printf("Enter height (cm): ");
    scanf("%lf", &height);
    
    height = height / 100;
    
    bmi = weight / (height * height);

    printf("Your BMI is %f\n", &bmi); /// <<< HERE!!! REMOVE &bmi to bmi.
    
    return 0;
}

 

and remove that ampersand from printf otherwise you are printing out garbage (a float interpretation of the 64bit address -> nonsense) 

Link to comment
Share on other sites

Link to post
Share on other sites

#include <stdio.h>

int main()
{
  double weight;
  double height;
  double bmi;
  
  printf("enter weight");
  scanf("%lf", &weight);
  
  printf("enter height");
  scanf("%lf", &height);
  
  bmi = weight/(height*height);
  printf("Your BMI is %lf\n", bmi);
  
  return 0;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×