-1

I am quite new to coding so I apologize for any amateur mistakes. I am attempting to program a problem that can calculate a persons Base Metabolic Rate based upon their gender, height, weight, age, and how much they exercise. In my attempt to do this, I tried to use two if statements. The first if statement says that if they select Male/Female, it will then run the user inputs through a certain algorithm. The second if statement is supposed to be for how often the user exercises. The second input is the one I am having difficult with. I cannot figure out how to multiply the BMR by the amount exercised. A lot of the time, the answer is coming up as 0. I'm really begining to think that my logic is screwed up. I will take any and all critiques, suggestions, advice. I just want to learn!

Thank you.

Here is my code

    namespace WindowsFormsApplication9
{
    public partial class calorieCalculator : Form
    {
        double malebmr, femalebmr, calories;
        int bmrmult;
        string gender, exercise;
        double height, weight, age;//variables

        public calorieCalculator()
        {
            InitializeComponent();
        }

        private void calorieCalculator_Load(object sender, EventArgs e)
        {



        }

        private void calculateButton_Click(object sender, EventArgs e)
        {



            //get personal information
            height = double.Parse(heightTextBox.Text);
            weight = double.Parse(weightTextBox.Text);
            age = double.Parse(ageTextBox.Text);
            //select gender
           if (genderList.SelectedIndex != -1)
            {
                gender = genderList.SelectedItem.ToString();
                switch (gender)
                {
                    case "Male":
                        //perform calculation
                        malebmr = (weight * 10 + height * 6.25 - age * 5 - 5);
                        calories = malebmr * bmrmult;
                        bmrDisplay.Text = ("Your base metabolic rate burns " + calories+ " calories");
                        break;
                    case "Female":
                        femalebmr = weight * 10 + height * 6.25 - age * 5 - 161;
                        MessageBox.Show("You should eat:" + femalebmr + "calories");
                        break;
                }
            }
           if (exerciseList.SelectedIndex !=-1)
            {
                string exercise;
                exercise = exerciseList.SelectedItem.ToString();
                switch (exercise)
                {
                                    case "Light exercise (1–3 days per week)":
                                       bmrmult = (decimal)1.375;
                               break;
                                      case "Moderate exercise (3–5 days per week)":
                                         bmrmult = (int)1.55m;
                                        break;
                                    case "Heavy exercise (6–7 days per week)":
                                        bmrmult = (int)1.725m;
                                        break;
                                     case "Very heavy exercise (twice per day, extra heavy workouts)":
                                         bmrmult = (int)1.9m;
                                        break;
                }
            }
        }

EDIT: Here are the formulas

if Male
BMR = (10 × weight in kg) + (6.25 × height in cm) - (5 × age in years) + 5

 if Female
BMR = (10 × weight in kg) + (6.25 × height in cm) - (5 × age in years) - 161


Little to no exercise   Daily kilocalories needed = BMR x 1.2
Light exercise (1–3 days per week)  Daily kilocalories needed = BMR x 1.375
Moderate exercise (3–5 days per week)   Daily kilocalories needed = BMR x 1.55
Heavy exercise (6–7 days per week)  Daily kilocalories needed = BMR x 1.725
Very heavy exercise (twice per day, extra heavy workouts)   Daily kilocalories needed = BMR x 1.9

EDIT 2: Here is my updated code!

 `using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication9
{
    public partial class calorieCalculator : Form
    {
        double malebmr, femalebmr, calories;
        int bmrmult;
        string gender, exercise;
        double height, weight, age;//variables

        public calorieCalculator()
        {
            InitializeComponent();
        }

        private void calorieCalculator_Load(object sender, EventArgs e)
        {



        }

        private void calculateButton_Click(object sender, EventArgs e)
        {



            //get personal information
            height = double.Parse(heightTextBox.Text);
            weight = double.Parse(weightTextBox.Text);
            age = double.Parse(ageTextBox.Text);
            //select gender
            if (exerciseList.SelectedIndex != -1)
            {
                string exercise;
                exercise = exerciseList.SelectedItem.ToString();
                switch (exercise)
                {
                    case "Light exercise (1–3 days per week)":
                        bmrmult = (int)1.375m;
                        break;
                    case "Moderate exercise (3–5 days per week)":
                        bmrmult = (int)1.55m;
                        break;
                    case "Heavy exercise (6–7 days per week)":
                        bmrmult = (int)1.725m;
                        break;
                    case "Very heavy exercise (twice per day, extra heavy workouts)":
                        bmrmult = (int)1.9m;
                        break;
                }
            }
            if (genderList.SelectedIndex != -1)
            {
                gender = genderList.SelectedItem.ToString();
                switch (gender)
                {
                    case "Male":
                        //perform calculation
                        malebmr = (weight * 10) + (height * 6.25) - (age * 5) - 5;
                        calories = malebmr * bmrmult;
                        bmrDisplay.Text = ("Your base metabolic rate burns " + calories + " calories");
                        break;
                    case "Female":
                        femalebmr = weight * 10 + height * 6.25 - age * 5 - 161;
                        MessageBox.Show("You should eat:" + femalebmr + "calories");
                        break;
                }
            }
        }
    }
}
Justin C
  • 3
  • 1
  • 5
  • Did you debug the code? Did you check which line of code can cause the problem? What is the formula of measuring BMR in general? Looking at the way you are doing calculation I think you need to correct it. – Chetan Nov 13 '17 at 00:57
  • `bmrmult` related code is executed at the last that's why it's value will be zero when you calculate BMR based on gender. That's why you get calorie value zero all the time `calories = malebmr * bmrmult` is zero coz bmrmult is zero. – Chetan Nov 13 '17 at 01:02
  • @ChetanRanpariya Good thought. Unfortunately, I switched the code around and I am still not getting the correct calculations. Here are my formulas. – Justin C Nov 13 '17 at 01:12
  • @ChetanRanpariya I updated the post with the formulas. – Justin C Nov 13 '17 at 01:14
  • Try putting the parenthesis the same way as formula in your code. `malebmr = (weight * 10) + (height * 6.25) - (age * 5) - 5` – Chetan Nov 13 '17 at 01:19
  • @ChetanRanpariya Still no luck. I'm beginning to think that the problem might lie in how I defined the if statement. It seems like whenever the user is selecting how often they exercise, the list is having no affect upon the answer. I think the problem is within this part `if (exerciseList.SelectedIndex != -1) { string exercise; exercise = exerciseList.SelectedItem.ToString(); switch (exercise) {` – Justin C Nov 13 '17 at 01:25
  • Debugging is the best friend. – Chetan Nov 13 '17 at 01:25
  • @ChetanRanpariya I have tried debugging it and it runs fine. It just is not calculating properly. Regardless, I appreciate your help with this matter! – Justin C Nov 13 '17 at 01:27
  • Debugging doesn't only mean to check if code runs fine. You need to check what values you are getting variables. Did you check what is assigned to `bmrmult` ? Did you put exercise related code before the gender related code? Why are you converting 1.375 to decimal and then assigning it to int variable? What is 1.55m? – Chetan Nov 13 '17 at 02:16
  • Whatever is the latest code after you made changes, can you update here? – Chetan Nov 13 '17 at 02:18
  • @ChetanRanpariya I have updated the post with my new code! I am not sure where I'm converting it to a decimal. However, there were a few times where I was unable to use the double variable in my formula. So I may have changed it then. 1.55 is supposed to be what the BMR is multiplied by if the user selects "Moderate exercise". Also, I'm not sure how to debug it other than by pressing f5... Thank you again. – Justin C Nov 13 '17 at 02:37
  • @ChetanRanpariya The program is correctly calculating `malebmr = (weight * 10) + (height * 6.25) - (age * 5) - 5` But I am still unable to get it to be multiplied by the bmrmult variable. – Justin C Nov 13 '17 at 02:40
  • To debug a specific line, put the cursor on that line and press F9. This will set a breakpoint on that line (brown circle and highlight). Then when you run with F5, it will stop on that line so you can see what all the variables have in them. Then you can also press F10 to go to the next line one step at a time. – BlueMonkMN Nov 13 '17 at 02:51

1 Answers1

0

When you cast the values to integer before putting them into bmrmult, you're eliminating any non-integer portion of the value. So bmrmult will become 1 in all cases. You should change the variable type of bmrmult to decimal or something that can hold decimal values, and remove the (int) casts.

I would also recommend switching based on exerciseList.SelectedIndex instead of exerciseList.SelectedItem.ToString() because it's more likely that your strings will change than your sequence of elements, and it's more likely there'll be a typo in the full text than in the index in the list.

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
  • Now I'm getting Error CS0019 Operator '*' cannot be applied to operands of type 'double' and 'decimal' – Justin C Nov 13 '17 at 04:50
  • You have 3 choices: 1) Change bmrmult to double instead of decimal; 2) change every "double" in your program to "decimal"; 3) apply explicit casts in your calculation like `calories = malebmr * (double)bmrmult`. – BlueMonkMN Nov 13 '17 at 15:03
  • See also https://stackoverflow.com/questions/8903632/operator-cannot-be-applied-to-operands-of-type-double-and-decimal and https://stackoverflow.com/questions/1165761/decimal-vs-double-which-one-should-i-use-and-when. I believe the reason you can't multiply these is because they deal with different trade-offs, and the compiler can't guess which set of trade-offs you need in the resulting value when you multiply one of each. – BlueMonkMN Nov 13 '17 at 15:09