0

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

Foxsneaks
  • 7
  • 2
  • You are reading in lines from console in many different spots depending on the workflow. If at any point you must be able to exit then any input you receive could be "exit". If someone is at the stage of "enter your height" then a valid input could be "exit" In your current code this would through exception because you're expecting float. I'd suggest considering making a private helper that reads lines in and checks if the line that was read in is "exit" and if it is exit the program, if not then return the line read – knowonecanknow Nov 23 '20 at 01:20
  • I know this doesn't really address your issue, what would prevent a user from pressing the close and or doing something like: `ALT+F4`, if they want out, they can. – Trevor Nov 23 '20 at 01:21
  • This sounds like https://stackoverflow.com/questions/52431607/exit-console-app-at-any-time-during-any-input-c – Flydog57 Nov 23 '20 at 03:20
  • @Codexer it is a very fair point, and I also mentioned the exact same thing. But apparently, we have to be able to code it into it. No idea why. Thanks for your response. – Foxsneaks Nov 23 '20 at 07:33

1 Answers1

0

So if you want to allow the user to enter a text such as "exit" to quit the program at any stage, then at every point where you read input you need to support this.

The way your code is structured at the moment then this is difficult as you basically have 1 method in your code which is doing everything. And you are actually mixing your "calculator" together with your "menu", which should be separated.

And if you look at your code then you will actually see that you are repeating sections of code multiple times with just slight differences (Writing and reading to/from console). Instead, you need to structure your code so that you have less repeat of code and instead split this out into separate methods.

This is actually the crux of your program. Because the way your code is structured, you can't easily adapt it to support the new requirement without re-writing it.

Now I'm not going to re-write your code for you, but I will give you hints on one way to partly do this (there are other ways!) Try and separate out the writing & reading into a separate method. Then you can also check for the word "exit" in a separate method.

So Pseudo code would be like this;

//do something in your method
var inputText = WriteAndGetInput("Please select option 1,2,3\r\nBlah blah blah \r\n");
CheckForExit(inputText);

//If you reach here, then Exit was not entered so carry on with your program and check for your real input.
//Try to use methods as to not repeat code.


//Method to write to console and get input
private static string WriteAndGetInput(string textToOutput)
{
       Console.WriteLine(textToOutput);
       return Console.ReadLine();  
}

//method to check if user typed exit
private static void CheckForExit(string inputText)
{
    if (inputText.Equals("exit"))
    {
        Console.WriteLine("You chose to exit.   Goodbye!");
        System.Environment.Exit(0); 
    }
}
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • 1
    Hey Jason! Thanks for your response! I have had a crack moving my code into different methods in hopes to make my code cleaner. I have also got rid of the useless mumbo jumbo that didn't need to be there as well. Now coming across more errors so back to Google for me! lol – Foxsneaks Nov 23 '20 at 07:32
  • Hey, sorry, one more thing. I have tried to input the last bit of code you mentioned for the user to be able to exit. I have added it as its own method but when I try to call the method I get error CS7036 'There is no argument given that corresponds to the required formal parameter of' – Foxsneaks Nov 23 '20 at 08:38