Search code examples
c#loopsif-statementmenubmi

BMI calculation and Menu logic


I am totally new to programming and have been racking my head around something that I know is so basic. I am not one to learn online so this is proving to be more difficult.

So I am needing to create a C# console app that has a menu where you have three options that include an 'exit'. So far I have only been able to run option 1 and 2 separately. When I view membership rates I can only choose option 1. Then when I tried to place coding for both membership and BMI calculation together I can still only run the membership!

Below is my code:

using System;

namespace BMITEST1MILLION
{
    class Program
    {
        static void Main(string[] args)

        {
            Console.WriteLine("Kiaora! Welcome to City Gym. Please select one of the following options:");
            Console.WriteLine("1.) View Our Membership Rates");
            Console.WriteLine("2.) Calculate My BMI");
            Console.WriteLine("3.) Exit");

            int num = Int32.Parse(Console.ReadLine());

            if (num == 1) ////condition.run if user main menu input is option 1 
            {
                Console.Clear();//clears previous screen

                Console.WriteLine("Which membership type would you like to view?"); //prompting user input
                Console.WriteLine("1.) Basic");
                Console.WriteLine("2.) Regular");
                Console.WriteLine("3.) Premium");


                int num1 = Int32.Parse(Console.ReadLine()); //declare if option 1 chosen
                if (num1 == 1) //condition. run if user input for membership type is 1
                {
                    Console.Clear(); //clears previous screen
                    Console.WriteLine("Our Basic Membership Rate is $10 per week or $40 per month");
                }


                int num2 = Int32.Parse(Console.ReadLine()); //declare if option 2 chosen
                if (num2 == 2) ; //condition. run if user input for membership type is 2
                {
                    Console.Clear(); //clears previous screen
                    Console.WriteLine("Our Regular Membership Rate is $15 per week or $60 per month");
                }

                int num3 = Int32.Parse(Console.ReadLine()); //declare if option 3 chosen
                if (num3 == 3) //condition. run if user input for membership type is 3

                {
                    Console.Clear(); //clears previous screen
                    Console.WriteLine("Our Premium Membership Rate is $20 per week or $80 per month");
                }


                if (num == 2)//condition.run if user main menu input is option 2 (BMI Calcuation)

                    //BMI calculation

                    Console.Write("Enter you height in metres (m):"); //ask user to input their height in metres
                double userHeight = Convert.ToDouble(Console.ReadLine()); // convert string to a double

                Console.Write("Enter you weight in kilograms (kg):"); //ask user to input their wedight in kilograms
                double userWeight = Convert.ToDouble(Console.ReadLine()); // convert string to a double

                double BMI = userWeight / (userHeight * userHeight); //method to calculate BMI based on user input weight and height
                Console.WriteLine("Your BMI is " + Math.Round(BMI, 2)); //print BMI result to screen. Round result to two decimals.

                if (BMI <= 18.5)//condition
                    Console.WriteLine("You are underweight.");

                else
                    if (BMI <= 25)//condition
                    Console.WriteLine("Your weight is normal.");

                else
                    if (BMI <= 30)//condition
                    Console.WriteLine("You are overweight.");

                else //if BMI is greater then 30
                    Console.WriteLine("You are obese.");

                Console.ReadKey();

            }//end of bmi calcuation

        }//main menu
    }//end of class
}//end of namespace

Solution

  • I recommend use of the do and switch-case:

    do {
      switch(option) {
        case 1: //do something
        break;
     
        case 2: //do something
        break;
    
        case 3: //exit
        break;
    
        default: // wrong option
        break;
      }
    }while(option != 3);
    

    Regards!