Ryan V. Ryan V. - 2 months ago 6
C# Question

Having incorrect solutions when program executes,

I've been working on a two class BMI Calculator for my class. I finally have managed to debug on get the program to execute. However I noticed that it wont let me enter height or weight as a decimal.

Example

putting 6.4 for height causes it to crash

Putting 6 height and 220 weight always seems to declare underweight and show incorrect bmi reading

example:

Height 6

Weight 220

BMI: score $0.09

It's calculating the BMI wrong. Looking at the code I can't seem to identify why its calculating this wrong.

-Edit- Managed to get it to take decimals! (Thank you everyone!) However it still seems to be messing up calculating the BMI

-Edit- Code Works!

(Code Part 1) Updated Mk2

using System;

namespace Calculator
{

public class BMICalc
{
private const
decimal REQ = 703;
private decimal weightPerson;
private decimal heightPerson;

public BMICalc()
{

}
public BMICalc(decimal weiP, decimal heiP)
{
this.weightPerson = weiP;
this.heightPerson = heiP;

}

public decimal SetBmi()
{
decimal bmi;
bmi = Convert.ToDecimal((this.weightPerson * 0.45m) / ((this.heightPerson * 12 * 0.025m) * (this.heightPerson * 12 * 0.025m)));

if (bmi < 18.5m)

{
Console.WriteLine("UnderWeight");
Console.ReadLine();
}
if (bmi > 18.5m && bmi < 25.0m)
{
Console.WriteLine("Normal");
Console.ReadLine();
}
if (bmi > 25.0m && bmi < 29.9m)
{
Console.WriteLine("OverWeight");
Console.ReadLine();
}
if (bmi > 29.9m && bmi < 40.0m)
{
Console.WriteLine("Obese");
Console.ReadLine();
}

return bmi;
}

public override string ToString()
{
return "\tCalculator" +
"\n\n Weight:" + this.weightPerson +
"\n\n Height:" + this.heightPerson +
"\n\n BMI Score:" + SetBmi().ToString();


}
}
}


Code (Part 2) Updated Mk 2

namespace Calculator
{
public class App
{
public static void Main() //
{
decimal heiP, weiP;

heiP = InputHeight();

weiP = InputWeight();



BMICalc bmiCal = new BMICalc(weiP, heiP);

Console.Clear();
Console.WriteLine(bmiCal.ToString());
Console.ReadKey();
}
public static decimal InputHeight()
{
decimal hNumber;

Console.Write("Please enter your height: ");
hNumber = Convert.ToDecimal(Console.ReadLine());
return hNumber;
}

public static decimal InputWeight()
{
Decimal wNumber;

Console.Write("Please enter your weight: ");

wNumber = Convert.ToDecimal(Console.ReadLine());
return wNumber;
}


}
}

Answer

You're input parameters into the constructor expects weight (weiP), height (heiP) (in that order) while you're passing height, weight from BMICalc bmiCal = new BMICalc(heiP, weiP);

That's the reason the value being output is incorrect. Interchange the parameters and it should be fine.

EDIT

Since you're entering the weight in Lbs and height in feet, you would need to convert feet into inches ( multiplying by 12) and then also multiplying the height and weight by the conversion metric.

So the formula would be:

bmi = Convert.ToDecimal((this.weightPerson * 0.45) / ((this.heightPerson * 12 * 0.025) * (this.heightPerson * 12 * 0.025)));

The currency comes in when it uses the ToString() overridden method. The format specified there is for currency.

Comments