-3

I'm trying to access the answer from an equation (BMR)

if(gender == "F")  
{   
    BMR = 655+(4.35 * weight) + (4.7 * height) - (4.7 * userAge );
}  
else if(gender == "M")  
{   
    BMR = 66+(6.23 * weight) + (12.7 * height) - (6.8 * userAge); 
}   
Console.WriteLine (name + " you entered: \nHeight: " + height + "\nWeight: " + weight + "\nAge: " + userAge + "\nGender: " + gender); 
Console.WriteLine ("Your BMR is " + BMR); 

and use it here

static void ProcessChoice (int c)
{   
    double allowedCalories;
    if (c == 1) {
        allowedCalories = BMR * 1.2;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 2) {
        allowedCalories = BMR * 1.375;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 3) {
        allowedCalories = BMR * 1.55;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 4) {
        allowedCalories = BMR * 1.725;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
    } else if (c == 5) {
        allowedCalories = BMR * 1.9;
        Console.WriteLine ("Your allowed calories is " + allowedCalories);
}

But I keep getting errors.

Here's the entire code:

using System;

namespace Manning_C__10_23_17_Lab_Five
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string name;  
            double height, weight;   
            int userAge;  
            string gender;  
            double BMR = 0; 

            Console.Write("Enter your name: ");  
            name = Console.ReadLine ();  
            Console.Write("Enter your height in inches: ");  
            height = Convert.ToDouble(Console.ReadLine ());  
            Console.Write ("Enter your weight in pounds: ");  
            weight = Convert.ToDouble(Console.ReadLine ());  
            Console.Write ("Enter your age: ");  
            userAge = Convert.ToInt32(Console.ReadLine ());  
            Console.Write ("Enter your gender as M or F ");  
            gender = Console.ReadLine ();  
            gender = gender.ToUpper(); 

            if(gender == "F")  
            {   
                BMR = 655+(4.35 * weight) + (4.7 * height) - (4.7 * userAge );
            }  
            else if(gender == "M")  
            {   
                BMR = 66+(6.23 * weight) + (12.7 * height) - (6.8 * userAge); 
            }   
            Console.WriteLine (name + " you entered: \nHeight: " + height + "\nWeight: " + weight + "\nAge: " + userAge + "\nGender: " + gender); 
            Console.WriteLine ("Your BMR is " + BMR); 

            int choice;
            do {
                PrintMenu ();
                choice = Int32.Parse (Console.ReadLine ());
                ProcessChoice (choice);
            } while (choice !=6);
            Console.WriteLine ("Thanks for using this system");
        }

        public static void PrintMenu()
        {
            Console.WriteLine("Main Menu");
            Console.WriteLine("1. You don't exercise");
            Console.WriteLine("2. You engage in light exercise one to three days a week");
            Console.WriteLine("3. You exercise moderately three to 5 times a week");
            Console.WriteLine("4. You exercise intensely six to seven days a week");
            Console.WriteLine("5. You exercise intensely six to seven days a week " +
                "and have a physically active job");
            Console.WriteLine ("6. QUIT");
        }

        static void ProcessChoice (int c)
        {
            double allowedCalories;
            if (c == 1) {
                allowedCalories = BMR * 1.2;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 2) {
                allowedCalories = BMR * 1.375;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 3) {
                allowedCalories = BMR * 1.55;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 4) {
                allowedCalories = BMR * 1.725;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            } else if (c == 5) {
                allowedCalories = BMR * 1.9;
                Console.WriteLine ("Your allowed calories is " + allowedCalories);
            }
        }
    }
}
4
  • 2
    What error are you getting?
    – Yonlif
    Commented Oct 23, 2017 at 21:47
  • 1
    What errors? What did you try to fix them? What does "I'm trying to access the answer from an equation (BMR)" mean? Commented Oct 23, 2017 at 21:47
  • 2
    Since BMR is defined inside of Main, it is not accessible outside of that method. You could declare it outside of main, at the class level: private double BMR = 0;, which allows all methods inside the class to access it, or you can include it in the method signature of any method that needs it: static void ProcessChoice(int c, double BMR)
    – Rufus L
    Commented Oct 23, 2017 at 21:49
  • Ohh I see the problem, just pass the BMR to the ProcessChoice function as well.
    – Yonlif
    Commented Oct 23, 2017 at 21:49

2 Answers 2

4

MBR is declared locally in main. You will not be able to use it outside of the main method in a direct manner.

There are multiple ways to solve this issue, but since your code consists of a single class (no dependency-injection between classes is necessary), two main ways come to mind:


First way:

You can declare it at a higher scope level (in this case, MainClass):

class MainClass
{
    double MBR = 0;
    //...

This makes the variable accessible to the entire class, including methods in it, which in turn includes ProcessChoice.


Second way:

You can pass it to ProcessChoice as a parameter:

static void ProcessChoice (int c, double MBR) {
    //...

and

int choice;
do {
    PrintMenu ();
    choice = Int32.Parse (Console.ReadLine ());
    ProcessChoice(choice, MBR);
} //...
1

You need to pass the BMR value to your ProcessChoice function:

    ProcessChoice (choice, BMR);

    static void ProcessChoice (int c, double BMR)
    {
         ....
    }
1
  • Considering the use of MBR, a better choice (IMHO) would be to declare it at a higher scope. That's why it's the first option I give in my own answer. Still, this is a reasonable answer. +1 --- My only suggestion would be to separate the snippets of code into two code-format blocks instead of one, just to avoid confusion. The code snippets are not continuous nor in the same scope, so avoid portraying them as such. 🙂 Commented Oct 23, 2017 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.