Search Unity

variable assigned but never used

Discussion in 'Scripting' started by abeyers, Mar 16, 2018.

  1. abeyers

    abeyers

    Joined:
    Mar 5, 2018
    Posts:
    8
    Hello, I'm working on an assignment for my intro to programming class where we have to create a BMI calculator and then return the user's allowed calorie intake. When trying to run my code I get the error saying The variable 'activityLevel' is assigned but never used. I'm a little confused because I thought I was using it on lines 41 and 44. I'm sure I'm doing something wrong, so if someone could help me correct my errors I would greatly appreciate it!

    Code (CSharp):
    1. using System;
    2.  
    3. //Austin Beyers
    4. //03/21/18
    5. //Lab 05
    6.  
    7. namespace Lab_02
    8. {
    9.     class MainClass
    10.     {
    11.         static double activityLevel;
    12.  
    13.         public static void Main (string[] args)
    14.         {
    15.             Console.WriteLine ("Hello and welcome to the BMR Calculator!");
    16.             Console.Write ("To begin, please enter your Name: ");
    17.             string name;
    18.             name = Console.ReadLine ();
    19.             Console.Write ("Thanks " + name + "! Now please enter your gender (m or f): ");
    20.             char gender;
    21.             gender = char.Parse (Console.ReadLine ());
    22.             Console.WriteLine ("Thanks. Now please enter the following information");
    23.             Console.Write ("Age: ");
    24.             int age;
    25.             age = Int32.Parse (Console.ReadLine ());
    26.             Console.Write ("Weight (in pounds): ");
    27.             int weight;
    28.             weight = Int32.Parse (Console.ReadLine ());
    29.             Console.Write ("Height (in inches): ");
    30.             int height;
    31.             height = Int32.Parse (Console.ReadLine ());
    32.             int mBMR;
    33.             mBMR = Convert.ToInt32 (66 + (6.23 * weight) + (12.7 * height) - (6.8 * age));
    34.             int fBMR;
    35.             fBMR = Convert.ToInt32 (655 + (4.35 * weight) + (4.7 * height) - (4.7 * age));
    36.             PrintMenu ();
    37.             int exerciseFactor = Int32.Parse(Console.ReadLine());
    38.             ProcessChoice(exerciseFactor);
    39.  
    40.             if (gender == 'm') {
    41.                 Console.WriteLine ("Your BMR is " + mBMR + ", and your allowed calories per day is " + (mBMR * activityLevel));
    42.             }
    43.             if (gender == 'f') {
    44.                 Console.WriteLine ("Your BMR is " + fBMR + ", and your allowed calories per day is " + (fBMR * activityLevel));
    45.             }
    46.  
    47.         }
    48.  
    49.         public static void PrintMenu()
    50.         {
    51.             Console.WriteLine ("What is your current Activity Level?");
    52.             Console.WriteLine ("1. You don't exercise");
    53.             Console.WriteLine ("2. You engage in light exercise one to three days a week");
    54.             Console.WriteLine ("3. You exercise moderately three to five times a week");
    55.             Console.WriteLine ("4. You exercise intensely six to seven days a week");
    56.             Console.WriteLine ("5. You exercise intensely six to seven days a week and have a physically active job");
    57.         }
    58.  
    59.         static void ProcessChoice (int ef)
    60.         {
    61.             if (ef == 1) {
    62.                 double activityLevel;
    63.                 activityLevel = 1.2;
    64.             }  else if (ef == 2) {
    65.                 double activityLevel;
    66.                 activityLevel = 1.375;
    67.             }  else if (ef==3){
    68.                 double activityLevel;
    69.                 activityLevel = 1.55;
    70.             }  else if (ef==4){
    71.                 double activityLevel;
    72.                 activityLevel = 1.725;
    73.             }  else if (ef==5){
    74.                 double activityLevel;
    75.                 activityLevel = 1.9;
    76.             }
    77.         }
    78.     }
    79. }
     
  2. gcaseres

    gcaseres

    Joined:
    Jun 17, 2015
    Posts:
    8
    This forum is specific to Unity C# Scripting, you should ask this kind of generic questions on other forums.

    Anyway, the problem in your code is that you have declared a static variable on line 11, and then you are declaring local variables with the same name in lines 62,65,68,71,74.

    The local variables are overriding the static variable in those contexts, so when you assign a value to that local variable, the compiler tells you that you are not using the local variables (you are just assigning values).

    The solution colud be to remove the lines 62,65,68,71,74, so the assigned variable is the static one declared in line 11.
     
    StickyHoneybuns likes this.
  3. StickyHoneybuns

    StickyHoneybuns

    Joined:
    Jan 16, 2018
    Posts:
    207
    Also, don't use static variables. You just learned somewhat why. Since you are new to programming just go ahead and get in the habit of not using them. Static variables are the lazy way to do something and can ruin an entire project if not done correctly. It also makes it extremely difficult to troublshoot as your project grows.

    From now own for just general coding questions that have nothing to do with Unity, use this site instead.

    https://stackoverflow.com