C#-error

error-"use of unassigned local variable"

Page 1 of 1

2 Replies - 1374 Views - Last Post: 18 December 2010 - 11:44 PM Rate Topic: -----

#1 morrow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-December 10

C#-error

Posted 18 December 2010 - 10:57 PM

I hope that someone can help me i have 2 errors that are the same but on different words if someone could help i'd apprieciate it.
Thanks, morrow

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class unit2project
{
    static void Main(string[] args)
    {
        string entry, name, gender;
        double weight, height, age, bmi, bmr, calrCalc;
        char genderOpt, levelOpt, hcOpt;
        // write opening Menu
        Console.Write("          Health Calculators           "); 
        Console.Write("\nOption 1: BMI (Body Mass Index)");
        Console.Write("\nOption 2: BMR (Basal Metabolic Rate)/ Calorie Needs)");
        Console.Write("\nOption 3: EXIT");
        Console.Write("\nPlease type 1 for Option 1, 2 for Option 2, 3 for Option 3");
        Console.Write("\nEnter one of the above option numbers: ");
        hcOpt = Convert.ToChar(Console.ReadLine());
        switch (Char.ToUpper(hcOpt))
        {
            case '1':                                           //if opt(ion) 1, do BMI Calculation
                Console.Write("                                    BMI Testing     ");
                Console.Write("\nBMI can be used to indicate if you are Underweight, Normal, Overweight, Obese");
                Console.Write("\nEnter Weight: ");
                weight = Convert.ToInt32(Console.ReadLine());
                Console.Write("(12 inches in 1 foot"); 
                Console.Write("Enter Height(inches please): "); 
                height = Convert.ToInt32(Console.ReadLine());
                //BMI Formula
                bmi = ((weight * 703) / (height * height));
                Console.WriteLine("BMI = {0}", bmi); 
                if (bmi < 18.5)                                //BMI less than 18.5 = underweight
                {
                    Console.WriteLine("According to my calculations you are Underweight");
                }
                if ((bmi == 18.5) || (bmi <= 24.9));           //BMI 18.5 to 24.9 = normal
                {
                    Console.WriteLine("According to my calculations you are Normal");
                }
                if ((bmi == 25) || (bmi <= 29.9)) ;            //BMI 25 to 29.9 = overweight
                {
                    Console.WriteLine("According to calculations you are Overweight");
                }
                if (bmi >= 30) ;                               //BMI = 30 or above = obese
                {
                    Console.WriteLine("According to calculations you are Obese");
                }
                break;
            case '3':
                Console.Clear();
                break;
            case '2':
                Console.Write("                                     BMR/Calorie Needs Testing     ");
                Console.Write("\nBMR stands for Basal Metabolic Rate, once you know you BMR, you can calculate your Daily Calorie Needs");
                Console.Write("\nEnter Weight: ");
                weight = Convert.ToInt32(Console.ReadLine());
                Console.Write("   (12 inches in 1 foot)"); 
                Console.Write("Please Enter Height in Inches: "); 
                height = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter Age: ");
                age = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter Gender: ");
                Console.Write("Please Type M for male and F for female");
                genderOpt = Convert.ToChar(Console.ReadLine());
                switch (Char.ToUpper(genderOpt))
                {
                    case 'F':
                        bmr = 665 + (4.35 * weight) + (4.7 * height) - (4.7 * age);
                        break;
                    case 'M':
                        bmr = 66 + (6.23 * weight) + (12.7 * height) - (6.8 * age);
                        break;
                }
                Console.Write("Please type one of the following rates of exercising: ");
                levelOpt = Convert.ToChar(Console.ReadLine());
                Console.Write("Please type 1 for little or no exercise");
                Console.Write("Please type 2 for light exercise/sports 1-3 days per week");
                Console.Write("Please type 3 for moderate exercise/sports 3 to 5 days per week");
                Console.Write("Please type 4 for hard exercise/ sports 6 to 7 days per week");
                Console.Write("Please type 5 for very hard exercise/sports & physical job or 2x training");
                switch (Char.ToUpper(genderOpt))
                {
                    case '1':
                        calrCalc = bmr * 1.2;
                        break;
                    case '2':
                        calrCalc = bmr * 1.375;
                        break;
                    case '3':
                        calrCalc = bmr * 1.55;
                        break;
                    case '4':
                        calrCalc = bmr * 1.725;
                        break;
                    case '5': 
                        calrCalc = bmr * 1.9;
                        break;
                }
                Console.WriteLine("Your BMR is {0} and you total daily calorie needs are {1}.", bmr, calrCalc);
                break;
            }
            Console.ReadLine();
    }
}



I am getting the error at the end of my coding

switch (Char.ToUpper(genderOpt))
                {
                    case '1':
                        calrCalc = bmr * 1.2;                //error on bmr
                        break;
                    case '2':
                        calrCalc = bmr * 1.375;
                        break;
                    case '3':
                        calrCalc = bmr * 1.55;
                        break;
                    case '4':
                        calrCalc = bmr * 1.725;
                        break;
                    case '5': 
                        calrCalc = bmr * 1.9;
                        break;
                }
                
Console.WriteLine("Your BMR is {0} and you total daily calorie needs are {1}.", bmr, calrCalc); //error-calrCalc
                break;



?? i have it labeled above

This post has been edited by Martyr2: 18 December 2010 - 11:47 PM
Reason for edit:: Please use code tags in the future, thanks!


Is This A Good Question/Topic? 0
  • +

Replies To: C#-error

#2 morrow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-December 10

Re: C#-error

Posted 18 December 2010 - 11:34 PM

i figured out how to fix the calrCalc error but i still can't figure out the bmr error please help
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3911
  • View blog
  • Posts: 11,448
  • Joined: 18-April 07

Re: C#-error

Posted 18 December 2010 - 11:44 PM

You have a few problems here I will address. On some of your if statements you have a semicolon right after the condition. You may want to remove those. For example you have the lines...

if ((bmi == 18.5) || (bmi <= 24.9)); // <-- Get rid of that semicolon... no semicolons at end of if statement conditions.
{



Then you have some variables that are not being used like "gender" or "name" or "entry". This is because you use other variables. So if you are not using those three strings, might want to delete them.

Now for your 2 errors. The reason it is flagging those variables is because there are a few situations where you use them in an equation or statement before they have been given an actual value. Lets assume that your first switch statement (where it checks the option for 'M' or 'F') never matches any of the switch statements. bmr will never get a value before it is used in your second switch case and applied in your equation calrCalc = bmr * 1.2;. Same with calrCalc. If that switch case never matches one of its cases, calrCalc will never be assigned a value.

To fix this you can add a "default" case to your switch statements or give those variables an initial value when you create them. So pick one of the following solutions...

switch (Char.ToUpper(genderOpt))
{
    case '1':
    calrCalc = bmr * 1.2;
    break;
    case '2':
    calrCalc = bmr * 1.375;
    break;
    case '3':
    calrCalc = bmr * 1.55;
    break;
    case '4':
    calrCalc = bmr * 1.725;
    break;
    case '5':
    calrCalc = bmr * 1.9;
    break;
    default: //<---- Notice the default case
    calrCalc = 0.0;
    break;
}



Or you can set a value when you create the variables...

double weight, height, age, bmi, bmr = 0.0, calrCalc = 0.0; //<--- Notice the values assigned to bmr and calrCalc when created.



These changes will make sure that you never reach an equation involving these two variables where it will never have a value.

Good luck and enjoy! :)
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1