-1

Here is my coding project. I'm almost done, but need help with this error. I've been getting the error "not all code paths return a value" and I don't understand why this is happening?

using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace Lab4
{
    /// <summary>
    /// 
    /// </summary>
    public sealed partial class MainPage : Page
    {
        // variables
        string kgs = "kg";
        string meters = "m";
        string cms = "cm";
        string nonApplicable = "n/a";
        string bmi = "bmi";
        string mhr = "beats / minute";
        string bmr = "calories";

        public MainPage() => this.InitializeComponent();

        // when anyone of the formulas are selected, change the unit to the proper one, then enable/disable and empty certain inputs
        private void cboFormulas_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cboFormulas.SelectedIndex == 0)
            {
                txtWeightUnit.Text = kgs;
                txtHeightUnit.Text = meters;
                txtformulaUnit.Text = bmi;

                txtAge.Text = String.Empty;
                txtFormulaCalc.Text = String.Empty;

                radGenderM.IsEnabled = false;
                radGenderF.IsEnabled = false;
                txtAge.IsEnabled = false;
                txtWeight.IsEnabled = true;
                txtHeight.IsEnabled = true;
            }

            if (cboFormulas.SelectedIndex == 1)
            {
                txtWeightUnit.Text = nonApplicable;
                txtHeightUnit.Text = nonApplicable;
                txtformulaUnit.Text = mhr;

                radGenderF.IsChecked = false;
                radGenderM.IsChecked = false;
                txtWeight.Text = String.Empty;
                txtHeight.Text = String.Empty;
                txtFormulaCalc.Text = String.Empty;

                
                radGenderM.IsEnabled = false;
                radGenderF.IsEnabled = false;
                txtAge.IsEnabled = true;
                txtWeight.IsEnabled = false;
                txtHeight.IsEnabled = false;
            }

            else if (cboFormulas.SelectedIndex == 2)
            {
                txtWeightUnit.Text = kgs;
                txtHeightUnit.Text = cms;
                txtformulaUnit.Text = bmr;

                txtFormulaCalc.Text = String.Empty;

                radGenderM.IsEnabled = true;
                radGenderF.IsEnabled = true;
                txtAge.IsEnabled = true;
                txtWeight.IsEnabled = true;
                txtHeight.IsEnabled = true;
            }
        }

        // if button is clicked, check which formula is selected, perform the calculation for said formula, and show the final answer
        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            if (cboFormulas.SelectedIndex == 0)
            {
                double inputWeight = Convert.ToDouble(txtWeight.Text);
                double inputHeight = Convert.ToDouble(txtHeight.Text);
                double bmiResult = bmiFormula(inputWeight, inputHeight);

                txtFormulaCalc.Text = Convert.ToString(bmiResult);
            }

            else if (cboFormulas.SelectedIndex == 1)
            {
                double inputAge = Convert.ToDouble(txtAge.Text);
                double mhrResult = mhrFormula(inputAge);

                txtFormulaCalc.Text = Convert.ToString(mhrResult);
            }

            else if (cboFormulas.SelectedIndex == 2)
            {
                double inputAge = Convert.ToDouble(txtAge.Text);
                double inputWeight = Convert.ToDouble(txtWeight.Text);
                double inputHeight = Convert.ToDouble(txtHeight.Text);
                double bmiResult = bmrFormula(inputAge, inputWeight, inputHeight);

                txtFormulaCalc.Text = Convert.ToString(bmiResult);
                //txtFormulaCalc.Text = Conve;
            }
        }

        // BMI formula
        private double bmiFormula(double weight, double height)
        {
            double bmiSum = weight / Math.Pow(height, 2);
            return bmiSum;
        }

        // MHR formula
        private double mhrFormula(double age)
        {
            double mhrSum = 208 - (0.7 * age);
            return mhrSum;
        }

        // BMR formula
        private double bmrFormula(double age, double weight, double height)
        {
            // perform male bmr forumla
            if (radGenderM.IsChecked == true)
            {
                double bmrSum = (66.47 + (13.75 * weight) + (5 * height) - (6.75 * age));
                return bmrSum;
            }

            // perform female bmr formula
            else if (radGenderF.IsChecked == true)
            {
                double bmrSum = (665.09 + (9.56 * weight) + (1.84 * height) - (4.67 * age));
                return bmrSum;
            }
        }
    }
}

This is where I seem to be getting the error which I don't understand why?

private double bmrFormula(double age, double weight, double height)
        {
            // perform male bmr forumla
            if (radGenderM.IsChecked == true)
            {
                double bmrSum = (66.47 + (13.75 * weight) + (5 * height) - (6.75 * age));
                return bmrSum;
            }

            // perform female bmr formula
            else if (radGenderF.IsChecked == true)
            {
                double bmrSum = (665.09 + (9.56 * weight) + (1.84 * height) - (4.67 * age));
                return bmrSum;
            }
        }
    }
}

I've tried multiple times trying to fix it, but I've run out of ideas. Any help with this problem would be much appreciated, but please explain how to how to fix it and not just the answer.

bdickens
  • 1
  • 2
  • If neither `radGenderM` nor `radGenderF` are checked, what value does your function return? –  Mar 14 '21 at 04:07

1 Answers1

2

bmrFormula must return a double. However, it's possible for the function to be called and not return a value. Your if statement lists two conditions, and if neither condition is true, the function will end without returning a value. If the only two options are radGenderM and radGenderF, change the else if to else. If there are more options, you will need to do one of the following

  • add an else if for each option
  • add an else at the end for a default value
  • return a default value after the if... else statement
phooBarred
  • 73
  • 7
  • It is generally a good practice to have a return statement (with a default value) before the end of the function, just in case :) – nurchi Mar 14 '21 at 04:20
  • 1
    @nurchi No it isn't best practice to always add unreachable code. It's best practice to think through the logic of a function and ensure it returns correctly. – Charlieface Mar 14 '21 at 05:33
  • 1
    @Charlieface, I didn't say add unreachable code, sometimes you only care about specific cases, for anything else, instead of adding `else` clause, just return a default value (or throw an exception, as applicable). From @Moshe's answer, I tried to make the third point clearer... – nurchi Mar 15 '21 at 01:15
  • @nurchi agreed, that's what I would do. – phooBarred Mar 15 '21 at 12:44