Mariah Medina
Mariah Medina

Reputation: 11

How do I properly call this method in my main program?

This is a BMI calculator, that calculates either imperial or metric BMI based on what option the user chooses. I then need to display their BMI status (underweight, overweight, obese). I have a method in my Person class that I want to call in the main method, but Visual Studio underlines the bmi variable and says "use of unassigned local variable"

How can I fix the variable problem?

Here is the class code:

class Person
{
    double Height { get; set; }
    double Weight { get; set; }
    double Inches { get; set; }
    double Bmi { get; set; }

    public Person()
    {

    }

    public Person(double height, double weight, double bmi)
    {
        Height = height;
        Weight = weight;
        Bmi = bmi;
    }

    public Person (double height, double weight, double inches, double bmi)
    {
        Height = height;
        Weight = weight;
        Inches = inches;
        Bmi = bmi;
    }


    //calculates BMI using metric formula 
    public static double CalcMetric(double height, double weight)
    {
        double bmi = weight / Math.Pow(height, 2);
        return bmi;
    }

    //calculates BMI using imperial formula
    public static double CalcImperial(double height, double weight)
    {
        double bmi = weight / Math.Pow(height, 2) * 703;
        return bmi;
    }

    public void Status(double bmi)
    {
        if (bmi < 18.5)
        {
            Console.WriteLine("Your BMI status is underweight.");
        }
        else
          if (bmi < 24.9)
        {
            Console.WriteLine("Your BMI status is normal.");
        }
        else
          if (bmi < 29.9)
        {
            Console.WriteLine("Your BMI status is overweight.");
        }
        else
          if (bmi > 30)
        {
            Console.WriteLine("Your BMI status is obese.");
        }
    }

}
}

Here is the main program

class Program
{
    static void Main(string[] args)
    {
        Console.BackgroundColor = ConsoleColor.White; //set background
        Console.Clear();
        Console.ForegroundColor = ConsoleColor.Black; //set foreground

        string inputHeight;
        string inputWeight;
        string aValue;
        double choice;
        double height;
        double weight;
        double bmi;


        Person p = new Person();

        Console.WriteLine("Welcome to BMI Calculator. \nEnter 1 for imperial calculator, 2 for metric calculator or 3 to quit.");
        aValue = Console.ReadLine();
        choice = double.Parse(aValue);

        if (choice == 1)
        {
            Console.WriteLine("Enter height in inches: ");
            inputHeight = Console.ReadLine();

            Console.WriteLine("Enter weight in lbs: ");
            inputWeight = Console.ReadLine();

            height = double.Parse(inputHeight);
            weight = double.Parse(inputWeight);

            Console.WriteLine("Your BMI is: " + Person.CalcImperial(height, weight));
        }
        else
                if (choice == 2)
        {
            Console.WriteLine("Enter height in meters: ");
            inputHeight = Console.ReadLine();

            Console.WriteLine("Enter weight in kilos: ");
            inputWeight = Console.ReadLine();

            height = double.Parse(inputHeight);
            weight = double.Parse(inputWeight);

            Console.WriteLine("Your BMI is: " + Person.CalcMetric(height, weight));
        }
        else
                if (choice == 3)
        {
            Console.WriteLine("Goodbye!");
        }

        p.Status(bmi);



        Console.ReadKey();

            }



        }

Upvotes: 0

Views: 117

Answers (2)

Pranay Rana
Pranay Rana

Reputation: 176934

you are getting error because double bmi; is not initialized. As per guide line method member need to be initialized in method you cannot just declare it. so you need to do like this

double bmi=0;

check this : Are C# uninitialized variables dangerous?


there is one more issue in your code ,

//in your person class
public void Status(double bmi)
{
}

and you are doing p.Status(ref bmi);, you will not get updated value as double is value type change done in function not reflect actual value , you need to do like this ,

//in your person class
public void Status(ref double bmi)
{
}

with help of ref you will get value back from functio.


even better solution is to return value from function

//in your person class
public double Status()
{
     double bmi=0;
    //rest of code 
    return bmi;
}

in main bmi = p.Status();

Upvotes: 1

0xC0d3
0xC0d3

Reputation: 77

Bmi is a internal member(by default) of person class so you can not use the BMI directly instead use

Status(CalcMetric); or `status(CalcImperial);` *according to your requirement*

So that these function return the value of BMI during the function call.

Upvotes: 0

Related Questions