I am very new to programming in C# so bare with me. I am currently trying to make a block of code for a BMI Calculator within a menu based Console that allows the user to select from different options and it gives them different results.
I have not completed my code yet but everything has been relatively smooth sailing until I got to the point where I have been told - "There should also be an option to ’exit‘ or terminate at any stage when the program is running."
I have looked on multiple forums and have tried to find an answer, none of which I have been able to get to work with my code. I was hoping someone here would be able to help me out and point me in the right direction.
Here is the code I am currently working with.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BMI_Application
{
class Program
{
static void Main(string[] args)
{
bool showMenu = true;
while (showMenu)
{
showMenu = MainMenu();
}
}
private static void Calculator()
{
}
private static bool MainMenu()
{
string bmi = "You have selected the BMI Calculator";
float height;
float weight;
float sum1; // Heightx2
float sum2; // Weight / sum1
Console.WriteLine("Please select an option");
Console.WriteLine("1: BMI Calculator");
Console.WriteLine("2: Membership Rates");
Console.WriteLine("3: Close Menu");
Console.WriteLine("\r\nPlease enter 1, 2 or 3");
Console.WriteLine("\r\nYou can also write 'bye' at any time to leave");
switch (Console.ReadLine())
{
case "1":
Console.Clear();
Console.WriteLine(bmi);
Console.WriteLine("Please enter your Height in Metres");
height = float.Parse(Console.ReadLine());
Console.WriteLine("Please enter your Weight in Kilograms");
weight = float.Parse(Console.ReadLine());
sum1 = height * height;
sum2 = weight / sum1;
Console.WriteLine("Your BMI is : {0}", sum2); // Next part is - based on their BMI Level, writing if they are underweight etc
if (sum2 <= 18.5)
{
Console.WriteLine("Your BMI level indicates that your are 'Underweight' "); // Underweight
}
if (sum2 >= 18.5 && sum2 <= 25)
{
Console.WriteLine("Your BMI level indicates that you are 'Normal' weight"); // Normal
}
if (sum2 >= 25 && sum2 <= 30)
{
Console.WriteLine("Your BMI level indicates that you are 'Overweight' ");// Overweight
}
if (sum2 >= 30)
{
Console.WriteLine("Your BMI level indicates that you are 'Obese' "); // Obese
}
Console.WriteLine("\r\nPress any key to return back to main menu");
Console.ReadKey();
Console.Clear();
return true;
case "2":
Console.Clear();
Console.WriteLine("You have selected the Membership Rates");
Console.WriteLine("What Membership would you like?");
return true;
case "3":
Console.WriteLine("You have selected to close the menu, press any key to continue"); // Terminate
Console.ReadKey();
return false;
case "bye":
Console.WriteLine("Goodbye! Push any key to get out of here!");
Console.ReadKey();
return false;
default:
Console.Clear();
Console.WriteLine("That is not a valid number, please enter 1, 2 or 3"); // Not valid entry
Console.WriteLine(" ");
return true;
}
}
}
}
Any ideas of how I would be able to get a user to exit out of the console by entering the word 'exit' for example?
Thanks very much