-1

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
Filburt
  • 17,626
  • 12
  • 64
  • 115
sandra
  • 3
  • 2
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Apr 11 '22 at 12:21
  • *"I can still only run the membership"* - There's a condition checking `if (num == 1)`, and the logic in that condition (the "membership" option) is all contained within that block. Then there's nothing after that block. Did you simply forget to add more conditions for menu options `2` and `3`? – David Apr 11 '22 at 12:26
  • Use {} on if and else. Always. (For now). You are already shooting yourself in the foot by omitting them. – Fildor Apr 11 '22 at 12:39
  • Thank you David. I will have a look tonight on using the debugger, the tour and how to ask. – sandra Apr 11 '22 at 19:16
  • @David. With the if (num==1) this is referring to the main menu. if (num==2), I was trying to use this for menu option 2, calculate BMI. I am still to figure out how to create the exit for option 3 as I need to create a loop back to the main menu. I thought I would tackle that when I figure the rest out first. – sandra Apr 11 '22 at 19:22
  • thanks @fidor. Yes I had the {} on there when I created the BMI calculation on its own. I deleted them thinking it may have been the reason why it wouldn't run when choosing option 2 from the main menu. – sandra Apr 11 '22 at 19:24
  • @sandra: The check for `if (num == 2)` should be *after* the entire block for `if (num == 1)`. Currently it's *inside* that block, so unless you change the value of `num` within that block then it would never be invoked. Check for menu item values at the same logic level, not nested within each other. – David Apr 11 '22 at 19:45

2 Answers2

0

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!

cahebebe
  • 16
  • 1
0

It seems like the structure of your main if statements is a little off. In order to execute either option 1 or option 2, it would be as

if(num == 1){
  ...
}
if(num == 2){
  ...
}

but you have

if(num == 1){
  ...
  if(num == 2)
}

Maybe this is what you meant by placing the code together? In this case, since you don't have braces following the if(num == 2) it will only apply to the line immeditately below it, ie the line that prints "Enter you height in metres (m):". However, since it only checks this condition if num already equals 1 (because it's nested in the num == 1 block), that line will never execute, so it skips that line and continues to double userHeight = Convert.ToDouble(Console.ReadLine());. Hope this helps!

jbsiefken
  • 26
  • 2