How to go back from a switch? c# - c#

After I've added credits I want it to go back to display the main menu again. What is the way to do this? And is there an easier way? I am not very experienced in programming.
Console.WriteLine("-------------------------------");
Console.WriteLine("");
Console.WriteLine(" VENDING MACHINIES LTD");
Console.WriteLine("");
Console.WriteLine("-------------------------------");
Console.WriteLine(" Main Menu");
Console.WriteLine("");
Console.WriteLine("1 - Add Credits (current credits= 0.00)");
Console.WriteLine("2 - Select product/s");
Console.WriteLine("3 - Exit");
Cons

Try this code:
var returnBack = true;
do
{
Console.WriteLine("-------------------------------");
Console.WriteLine("");
Console.WriteLine(" VENDING MACHINIES LTD");
Console.WriteLine("");
Console.WriteLine("-------------------------------");
Console.WriteLine(" Main Menu");
Console.WriteLine("");
Console.WriteLine("1 - Add Credits (current credits= 0.00)");
Console.WriteLine("2 - Select product/s");
Console.WriteLine("3 - Exit");
Console.WriteLine("");
Console.Write("Please Enter A Number: ");
choice = Convert.ToInt32(Console.ReadLine());
while (choice < 1 || choice > 3)
{
Console.Write("Incorrect option. Please Re-Enter: ");
choice = Convert.ToInt32(Console.ReadLine());
}
switch (choice)
{
case 1:
Console.Write("How Many Credits Would You Like To Add? ");
credits = Convert.ToInt32(Console.ReadLine());
Console.Write("You Now Have {0} Credits", credits);
Console.WriteLine("");
Console.Write("Press return to go back to the main menu");
Console.ReadLine();
break;
case 2:
Console.WriteLine();
break;
case 3:
returnBack=false;
break;
}
} while (returnBack);

You can use while(true) for eternity cycle of menu and return for exit from your application
Console.WriteLine("-------------------------------");
Console.WriteLine("");
Console.WriteLine(" VENDING MACHINIES LTD");
Console.WriteLine("");
Console.WriteLine("-------------------------------");
while(true)
{
Console.WriteLine(" Main Menu");
Console.WriteLine("");
Console.WriteLine("1 - Add Credits (current credits= 0.00)");
Console.WriteLine("2 - Select product/s");
Console.WriteLine("3 - Exit");
Console.WriteLine("");
Console.Write("Please Enter A Number: ");
choice = Convert.ToInt32(Console.ReadLine());
while (choice < 1 || choice > 3)
{
Console.Write("Incorrect option. Please Re-Enter: ");
choice = Convert.ToInt32(Console.ReadLine());
}
switch (choice)
{
case 1:
Console.Write("How Many Credits Would You Like To Add? ");
credits = Convert.ToInt32(Console.ReadLine());
Console.Write("You Now Have {0} Credits", credits);
Console.WriteLine("");
Console.Write("Press return to go back to the main menu");
Console.ReadLine();
break;
case 2:
Console.WriteLine();
break;
case 3:
Console.WriteLine("Bye-bye");
return;
}
}

You can use a recursive method and I would also suggest to extract the "menu" console write lines. Please have a look on the following:
static void Main(string[] args)
{
DisplayMenu();
var choice = Convert.ToInt32(Console.ReadLine());
// Final response
choice = DisplayMenu(choice);
}
public static void DisplayMenu()
{
Console.WriteLine(" Main Menu");
Console.WriteLine("");
Console.WriteLine("1 - Add Credits (current credits= 0.00)");
Console.WriteLine("2 - Select product/s");
Console.WriteLine("3 - Exit");
Console.WriteLine("");
Console.Write("Please Enter A Number: ");
}
// Recursive overload
public static int DisplayMenu(int choice)
{
// If invalid input
if (choice<1 || choice>3)
{
Console.Write("Incorrect option. Please Re-Enter: ");
Console.WriteLine();
DisplayMenu();
return DisplayMenu(Convert.ToInt32(Console.ReadLine()));
}
// If valid show one more time the menu
DisplayMenu();
return Convert.ToInt32(Console.ReadLine());
}

Related

How do I add a changing history of ten results in a calculator in C#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a simple calculator and I want to make a history of the ten previous results.
I know it is built in a weird way but I am still a beginner so don't expect too much. Please tell me how to do it and obviously explain what everything does. I just cannot find a solution. I am learning about arrays and lists but I can't figure out what to do.
class Program
{
public static void Main(string[] args)
{
start:
Console.WriteLine("this is a simple calculator. you will need to choose what kind of math problem you want and how many numbers you want");
Console.WriteLine("1. division");
Console.WriteLine("2. multiplication");
Console.WriteLine("3. addition ");
Console.WriteLine("4. subtraction");
Console.WriteLine("5. exponent");
int answermenu = int.Parse(Console.ReadLine());
switch (answermenu)
{
case 1: goto divider;
case 2: goto multiplier;
case 3: goto addition;
case 4: goto subtracter;
case 5: goto exponent;
default:
Console.WriteLine("You didn't pick any of the options. Please pick a valid option when we get you back to the menu.");
Console.WriteLine("to continue press enter");
Console.ReadKey();
goto start;
}
divider:
Console.WriteLine("welcome to the division problem");
Console.WriteLine("how many numbers you want to include in your math problem?");
Console.WriteLine("write the number you want and then press enter.");
int HMNdivanswer = int.Parse(Console.ReadLine());
switch (HMNdivanswer)
{
case 2: goto divider2nums;
case 3: goto divider3nums;
case 4: goto divider4nums;
case 5: goto divider5nums;
case 6: goto divider6nums;
default:
Console.WriteLine("you didn't provide a valid number. please write a valid number. the max number is 6 and the min number is 2.");
goto divider;
}
divider2nums:
Console.Write("please write num1: ");
double num1division2nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2division2nums = int.Parse(Console.ReadLine());
double lastanswer = num1division2nums / num2division2nums;
Console.WriteLine($"the answer is " + num1division2nums / num2division2nums);
Console.ReadKey();
goto backtomenuanswerdivision;
divider3nums:
Console.Write("please write num1: ");
double num1division3nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2division3nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3division3nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1division3nums / num2division3nums / num3division3nums);
goto backtomenuanswerdivision;
divider4nums:
Console.Write("please write num1: ");
double num1division4nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2division4nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3division4nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4division4nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1division4nums / num2division4nums / num3division4nums / num4division4nums);
goto backtomenuanswerdivision;
divider5nums:
Console.Write("please write num1: ");
double num1division5nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2division5nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3division5nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4division5nums = int.Parse(Console.ReadLine());
Console.Write("please write num5: ");
double num5division5nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1division5nums / num2division5nums / num3division5nums / num4division5nums / num5division5nums);
goto backtomenuanswerdivision;
divider6nums:
Console.Write("please write num1: ");
double num1division6nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2division6nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3division6nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4division6nums = int.Parse(Console.ReadLine());
Console.Write("please write num5: ");
double num5division6nums = int.Parse(Console.ReadLine());
Console.Write("please write num6: ");
double num6division6nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1division6nums / num2division6nums / num3division6nums / num4division6nums / num5division6nums / num6division6nums);
goto backtomenuanswerdivision;
backtomenuanswerdivision:
Console.WriteLine("where do you want to go now?");
Console.WriteLine("1. main menu");
Console.WriteLine("2. division menu");
Console.WriteLine("3. end program");
int menutomenuanswerdivision = int.Parse(Console.ReadLine());
switch (menutomenuanswerdivision)
{
case 1: goto start;
case 2: goto divider;
case 3: goto end_program;
default:
Console.WriteLine(" you didn't pick any of the options.please pick a valid option when we get you back to the menu.");
Console.WriteLine("to continue press enter");
Console.ReadKey();
goto backtomenuanswerdivision;
}
multiplier:
Console.WriteLine("welcome to the multiplying problem");
Console.WriteLine("how many numbers you want to include in your math problem?");
Console.WriteLine("write the number you want and then press enter.");
int HMNmulanswer = int.Parse(Console.ReadLine());
switch (HMNmulanswer)
{
case 2: goto multiplier2nums;
case 3: goto multiplier3nums;
case 4: goto multiplier4nums;
case 5: goto multiplier5nums;
case 6: goto multiplier6nums;
default:
Console.WriteLine("you didnt write a valid number. please write a valid number");
goto multiplier;
}
multiplier2nums:
Console.Write("please write num1: ");
double num1multiplier2nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2multiplier2nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1multiplier2nums * num2multiplier2nums);
goto BackToMenuAnswerMultiplier;
multiplier3nums:
Console.Write("please write num1: ");
double num1multiplier3nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2multiplier3nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3multiplier3nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1multiplier3nums * num2multiplier3nums * num3multiplier3nums);
goto BackToMenuAnswerMultiplier;
multiplier4nums:
Console.Write("please write num1: ");
double num1multiplier4nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2multiplier4nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3multiplier4nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4multiplier4nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1multiplier4nums * num2multiplier4nums * num3multiplier4nums * num4multiplier4nums);
goto BackToMenuAnswerMultiplier;
multiplier5nums:
Console.Write("please write num1: ");
double num1multiplier5nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2multiplier5nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3multiplier5nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4multiplier5nums = int.Parse(Console.ReadLine());
Console.Write("please write num5: ");
double num5multiplier5nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1multiplier5nums * num2multiplier5nums * num3multiplier5nums * num4multiplier5nums * num5multiplier5nums);
goto BackToMenuAnswerMultiplier;
multiplier6nums:
Console.Write("please write num1: ");
double num1multiplier6nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2multiplier6nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3multiplier6nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4multiplier6nums = int.Parse(Console.ReadLine());
Console.Write("please write num5: ");
double num5multiplier6nums = int.Parse(Console.ReadLine());
Console.Write("please write num6: ");
double num6multiplier6nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + num1multiplier6nums * num2multiplier6nums * num3multiplier6nums * num4multiplier6nums * num5multiplier6nums * num6multiplier6nums);
goto BackToMenuAnswerMultiplier;
BackToMenuAnswerMultiplier:
Console.WriteLine("where do you want to go now?");
Console.WriteLine("1. main menu");
Console.WriteLine("2. multiplying menu");
Console.WriteLine("3. end program");
int menutomenuanswermultiplier = int.Parse(Console.ReadLine());
switch (menutomenuanswermultiplier)
{
case 1: goto start;
case 2: goto multiplier;
case 3: goto end_program;
default:
Console.WriteLine(" you didn't pick any of the options. Please pick a valid option when we get you back to the menu.");
Console.WriteLine("to continue press enter");
Console.ReadKey();
goto BackToMenuAnswerMultiplier;
}
addition:
Console.WriteLine("welcome to the addition problem");
Console.WriteLine("how many numbers you want to include in your math problem?");
Console.WriteLine("write the number you want and then press enter.");
int HMNaddanswer = int.Parse(Console.ReadLine());
switch(HMNaddanswer)
{
case 2: goto addition2nums;
case 3: goto addition3nums;
case 4: goto addition4nums;
case 5: goto addition5nums;
case 6: goto addition6nums;
default: Console.WriteLine("you didn't provide a valid number. please write a valid number");
goto addition;
}
addition2nums:
Console.Write("please write num1: ");
double num1addition2nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2addition2nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1addition2nums + num2addition2nums));
goto BackToMenuAnswerAddition;
addition3nums:
Console.Write("please write num1: ");
double num1addition3nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2addition3nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3addition3nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1addition3nums + num2addition3nums + num3addition3nums));
goto BackToMenuAnswerAddition;
addition4nums:
Console.Write("please write num1: ");
double num1addition4nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2addition4nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3addition4nums = int.Parse(Console.ReadLine());
Console.WriteLine("please write num4: ");
double num4addition4nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1addition4nums + num2addition4nums + num3addition4nums + num4addition4nums));
goto BackToMenuAnswerAddition;
addition5nums:
Console.Write("please write num1: ");
double num1add5nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2add5nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3add5nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4add5nums = int.Parse(Console.ReadLine());
Console.Write("please write num5: ");
double num5add5nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1add5nums + num2add5nums + num3add5nums + num4add5nums + num5add5nums));
goto BackToMenuAnswerAddition;
addition6nums:
Console.Write("please write num1: ");
double num1add6nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2add6nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3add6nums = int.Parse(Console.ReadLine());
Console.WriteLine("please write num4: ");
double num4add6nums = int.Parse(Console.ReadLine());
Console.WriteLine("please write num5: ");
double num5add6nums = int.Parse(Console.ReadLine());
Console.WriteLine("please write num6: ");
double num6add6nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1add6nums + num2add6nums + num3add6nums + num4add6nums + num5add6nums + num6add6nums));
goto BackToMenuAnswerAddition;
BackToMenuAnswerAddition:
Console.WriteLine("where do you want to go now?");
Console.WriteLine("1. main menu");
Console.WriteLine("2. addition menu");
Console.WriteLine("3. end program");
int MenuToMenuAnswerAddition = int.Parse(Console.ReadLine());
switch (MenuToMenuAnswerAddition)
{
case 1: goto start;
case 2: goto addition;
case 3: goto end_program;
default:
Console.WriteLine(" you didn't pick any of the options. Please pick a valid option when we get you back to the menu.");
Console.WriteLine("to continue press enter");
Console.ReadKey();
goto BackToMenuAnswerAddition;
}
subtracter:
Console.WriteLine("welcome to the subtraction problem");
Console.WriteLine("how many numbers you want to include in your math problem?");
Console.WriteLine("write the number you want and then press enter.");
int HMNsubanswer = int.Parse(Console.ReadLine());
switch (HMNsubanswer)
{
case 2: goto subtracter2nums;
case 3: goto subtracter3nums;
case 4: goto subtracter4nums;
case 5: goto subtracter5nums;
case 6: goto subtracter6nums;
}
subtracter2nums:
Console.Write("please write num1: ");
double num1subtraction2nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2subtraction2nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1subtraction2nums - num2subtraction2nums));
goto BackToMenuAnswerSubstraction;
subtracter3nums:
Console.Write("please write num1: ");
double num1subtraction3nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2subtraction3nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3subtraction3nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1subtraction3nums - num2subtraction3nums - num3subtraction3nums));
goto BackToMenuAnswerSubstraction;
subtracter4nums:
Console.Write("please write num1: ");
double num1subtraction4nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2subtraction4nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3subtraction4nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4subtraction4nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1subtraction4nums - num2subtraction4nums - num3subtraction4nums - num4subtraction4nums));
goto BackToMenuAnswerSubstraction;
subtracter5nums:
Console.Write("please write num1: ");
double num1subtraction5nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2subtraction5nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3subtraction5nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4subtraction5nums = int.Parse(Console.ReadLine());
Console.Write("please write num5: ");
double num5subtraction5nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1subtraction5nums - num2subtraction5nums - num3subtraction5nums - num4subtraction5nums - num5subtraction5nums));
goto BackToMenuAnswerSubstraction;
subtracter6nums:
Console.Write("please write num1: ");
double num1subtraction6nums = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2subtraction6nums = int.Parse(Console.ReadLine());
Console.Write("please write num3: ");
double num3subtraction6nums = int.Parse(Console.ReadLine());
Console.Write("please write num4: ");
double num4subtraction6nums = int.Parse(Console.ReadLine());
Console.Write("please write num5: ");
double num5subtraction6nums = int.Parse(Console.ReadLine());
Console.Write("please write num6: ");
double num6subtraction6nums = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + (num1subtraction6nums - num2subtraction6nums - num3subtraction6nums - num4subtraction6nums - num5subtraction6nums - num6subtraction6nums));
goto BackToMenuAnswerSubstraction;
BackToMenuAnswerSubstraction:
Console.WriteLine("where do you want to go now?");
Console.WriteLine("1. main menu");
Console.WriteLine("2. subtraction menu");
Console.WriteLine("3. end program");
int MenuToMenuAnswerSubtraction = int.Parse(Console.ReadLine());
switch (MenuToMenuAnswerSubtraction)
{
case 1: goto start;
case 2: goto addition;
case 3: goto end_program;
default:
Console.WriteLine(" you didn't pick any of the options. Please pick a valid option when we get you back to the menu.");
Console.WriteLine("to continue press enter");
Console.ReadKey();
goto BackToMenuAnswerSubstraction;
}
exponent:
Console.WriteLine("welcome to the exponent problem");
Console.Write("please write num1: ");
double num1exponent = int.Parse(Console.ReadLine());
Console.Write("please write num2: ");
double num2exponent = int.Parse(Console.ReadLine());
Console.WriteLine($"the answer is " + Math.Pow(num1exponent,num2exponent));
BackToMenuAnswerExponent:
Console.WriteLine("where do you want to go now?");
Console.WriteLine("1. main menu");
Console.WriteLine("2. Exponent menu");
Console.WriteLine("3. end program");
int MenuToMenuAnswerExponent = int.Parse(Console.ReadLine());
switch (MenuToMenuAnswerExponent)
{
case 1: goto start;
case 2: goto exponent;
case 3: goto end_program;
default:
Console.WriteLine(" you didn't pick any of the options.please pick a valid option when we get you back to the menu.");
Console.WriteLine("to continue press enter");
Console.ReadKey();
goto BackToMenuAnswerExponent;
}
end_program:
return;
}
}
I guess this is for a school project, but at least you need to add some methods. At best some classes. goto statement is not to be used. Promise me that you will never use goto statements when you get a job.
But to try to answer your question, you could add a list of something, which you add to, everytime you perform an operation.
class Program
{
private static List<string> history = new List<String>();
public static void Main()
{
// all you code
// call AddHistory(...) for every operation being done
}
private static void AddHistory(string message)
{
history.Insert(0, message);
if(history.Count > 10)
history.Remove(10);
}
}
Not been tested, written by heart.

C# - loop makes my app not work propely

I have this app that I'm writing in C# just for practice purposes, this is the code,
using System;
namespace Stock_Control
{
class Program
{
static void Main(string[] args)
{
int qty, index;
string name, location, code;
Item item = new Item();
ConsoleKeyInfo opt;
do
{
Console.Clear();
Console.WriteLine();
Console.WriteLine("******************** STOCK CONTROL SOFTWARE *********************");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Chose a option:");
Console.WriteLine("1 - Add item.");
Console.WriteLine("2 - View Stock.");
Console.WriteLine("3 - Remove item.");
Console.WriteLine("4 - Press 'Escape' to exit.");
opt = Console.ReadKey(true);
switch (opt.KeyChar.ToString())
{
case "1":
Console.WriteLine();
Console.Write("Enter the item code:");
code = Console.ReadLine();
Console.Write("Enter the item quantity:");
qty = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the item name:");
name = Console.ReadLine();
Console.Write("Enter the item location:");
location = Console.ReadLine();
item.add(code, qty, name, location);
break;
case "2":
Console.WriteLine("teste");
//item.view();
break;
case "3":
Console.WriteLine();
Console.WriteLine("Enter the index of the item to be removed:");
index = Int32.Parse(Console.ReadLine());
item.remove(index);
break;
}
} while (opt.Key != ConsoleKey.Escape);
}
}
}
It works fine but for some reason, when i wrapped around a loop, the second case in the switch statement stop working, once i remove the loop everything works fine. I can't figured out whats wrong.
Can someone help me with that?
Thanks.
It works still just fine but when you added the loop you do your Console.WriteLine("teste"); item.view(); in case "2" and then immediately start again = clear the console so you do not see anything.

How to allow the user to choose whether the continue the program or exit it (C#)

I've written a fairly simple program that displays a menu of choices and performs calculations based on the user's input. I'd like to make the program behave so that it asks the user if they would like to continue back to the menue or exit the program instead. I'm thinking that it might require a loop of some kind, but I'm unsure as to how to implement it.
Here is my code
using System;
namespace WarmUpCalculations
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
Console.WriteLine("Welcome to the Chemistry Formula Calculator!\n\n\n");
Console.WriteLine("Press 1 for the Density Calculator");
Console.WriteLine("Press 2 for the Moles Calculator");
Console.WriteLine("Press 3 for the Energy of a Wave Calculator");
Console.WriteLine("Press 4 for the Ideal Gas Law Calculator\n\n");
Console.WriteLine("Please enter a Number from the Options above");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
DensityCalculator();
break;
case "2":
MolesCalculator();
break;
case "3":
EnergyOfWaveCalculator();
break;
case "4":
IdealGasLawCalculator();
break;
}
}
static void DensityCalculator()
{
Console.Clear();
Console.WriteLine("Density Calaculator\n\n");
Console.WriteLine("Will this be for Grams or Kilograms?");
Console.WriteLine("Type 'g' for Grams or 'kg' for Kilograms");
string unitMass = Console.ReadLine();
Console.WriteLine("Please Enter Your Mass");
Decimal Mass = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Enter units for Volume");
string unitVolume = Console.ReadLine();
Console.WriteLine("Please Enter Your Volume");
Decimal Volume = Convert.ToDecimal(Console.ReadLine());
Decimal Density = Mass / Volume;
Math.Round(Density, 4);
Console.Clear();
Console.WriteLine("Moles Calaculator\n\n");
Console.Write("Your Density is ");
Console.Write(Density);
Console.Write(unitMass);
Console.Write("/");
Console.WriteLine(unitVolume);
Console.Write(" \n\n");
}
static void MolesCalculator()
{
Console.Clear();
Console.WriteLine("Moles Calaculator\n\n");
Console.WriteLine("Please enter mass of sample");
Decimal Mass = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please Enter Your molar mass");
Decimal MolarMass = Convert.ToDecimal(Console.ReadLine());
Decimal Moles = Mass / MolarMass;
Console.Clear();
Console.WriteLine("Moles Calaculator\n\n");
Console.Write("Your sample has ");
Console.Write(Moles);
Console.Write(" moles\n\n");
}
static void EnergyOfWaveCalculator()
{
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.WriteLine("Please enter the frequency");
double Frequency = Convert.ToDouble(Console.ReadLine());
double PlancksConstant = 6.626e-34;
double Energy = PlancksConstant * Frequency;
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.Write("The answer is ");
Console.Write(Energy);
Console.Write(" \n\n");
}
static void IdealGasLawCalculator()
{
Console.Clear();
Console.WriteLine("Ideal Gas Law Calaculator\n\n");
Console.WriteLine("Would you like to solve the following equation for Pressure or Volume? Press v for Volume or p for Pressure");
string Frequency = Console.ReadLine();
if (Frequency == "v"){
Console.Clear();
Console.WriteLine("Ideal Gas Law Calaculator\n\n");
Console.WriteLine("Please enter the pressure");
decimal Pressure = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the number of moles");
decimal NumberOfMoles = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the temperature in degrees Kelvin");
decimal TemperatureKelvin = Convert.ToDecimal(Console.ReadLine());
decimal GasLawConstant = Convert.ToDecimal(8.314);
decimal IdealGasLaw = NumberOfMoles * GasLawConstant * TemperatureKelvin / Pressure;
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.Write("Your answer is ");
Console.Write(IdealGasLaw);
Console.Write(" \n\n");
}
else
{
Console.Clear();
Console.WriteLine("Ideal Gas Law Calaculator\n\n");
Console.WriteLine("Please enter the volume");
decimal Volume = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the number of moles");
decimal NumberOfMoles = Convert.ToDecimal(Console.ReadLine());
Console.WriteLine("Please enter the the temperature in degrees Kelvin");
decimal TemperatureKelvin = Convert.ToDecimal(Console.ReadLine());
decimal GasLawConstant = Convert.ToDecimal(8.314);
decimal IdealGasLaw = NumberOfMoles * GasLawConstant * TemperatureKelvin / Volume;
Console.Clear();
Console.WriteLine("Energy of Wave Calaculator\n\n");
Console.Write("Your answer is ");
Console.Write(IdealGasLaw);
Console.Write(" \n\n");
}
}
}
}
It's pretty straightforward. You had the right idea.
bool shouldContinue = true;
while(shouldContinue){
Console.WriteLine("Press 1 for the Density Calculator");
Console.WriteLine("Press 2 for the Moles Calculator");
Console.WriteLine("Press 3 for the Energy of a Wave Calculator");
Console.WriteLine("Press 4 for the Ideal Gas Law Calculator\n\n");
Console.WriteLine("Press 5 to exit");
Console.WriteLine("Please enter a Number from the Options above");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
DensityCalculator();
break;
case "2":
MolesCalculator();
break;
case "3":
EnergyOfWaveCalculator();
break;
case "4":
IdealGasLawCalculator();
break;
case "5":
shouldContinue = false;
break;
}
}
You can use the same logic as for calculator choice and put all your code inside loop.
Change your code in Main:
char action = 'Y'; //create varible for user choice (continue or not)
while (action == 'Y') // add loop
{
Console.Clear();
Console.WriteLine("Welcome to the Chemistry Formula Calculator!\n\n\n");
Console.WriteLine("Press 1 for the Density Calculator");
Console.WriteLine("Press 2 for the Moles Calculator");
Console.WriteLine("Press 3 for the Energy of a Wave Calculator");
Console.WriteLine("Press 4 for the Ideal Gas Law Calculator\n\n");
Console.WriteLine("Please enter a Number from the Options above");
string choice = Console.ReadLine();
switch (choice)
{
case "1":
DensityCalculator();
break;
case "2":
MolesCalculator();
break;
case "3":
EnergyOfWaveCalculator();
break;
case "4":
IdealGasLawCalculator();
break;
}
//add these lines
Console.WriteLine("Do you want to continue!\n\n\n");
Console.WriteLine("Press Y to continue");
Console.WriteLine("Press N to finish");
action = Console.ReadKey().KeyChar;
}
Or better option is:
Put all code from Main inside some method(i.e. Start()) and then in Main:
char action = 'Y';
while (action == 'Y')
{
Start();
Console.WriteLine("Do you want to continue!\n\n\n");
Console.WriteLine("Press Y to continue");
Console.WriteLine("Press N to finish");
action = Console.ReadKey().KeyChar;
}
You can define a new method Menu
Put the menu code in there and then just call upon it when you need it. At the end of Main() you can use a simple if case
Console.WriteLine("Type 'exit' to quit");
if(Console.ReadLine() == "exit")
{
}else
{
Menu();
}
Change your code like this.. there is too much code in the main method:
static void Main(string[] args)
{
ChooseCalculationMenu();
}
public void ChooseCalculationMenu()
{
// Put the code from Main here instead
}
and in the end of each calculation method:
Console.WriteLine("Press c to continue or q to quit.");
if (Console.Readline() == "q");
{}
else
{
ChooseCalculationMenu();
}

Vending machine repeat menu after option is chosen?

Here is my vending machine. I now need to be able to be able to repeat the menu at the end of the switch to allow the user to go back into the vending machine or exit the program.
Would I use a while loop or an if/else statement. I tried nesting the whole thing in a while loop but then it repeated the the purchase option.
int iNumCrisps = 10;
int iCrispsBought;
int iNumChocbars = 20;
int iChocbarsBought;
int iNumSweets = 30;
int iSweetsBought;
double dTotalMoney = 0;
int iChoice;
{
//display the choices
Console.WriteLine("Vending Machine");
Console.WriteLine("1 - Buy chocbars");
Console.WriteLine("2 - Buy crisps");
Console.WriteLine("3 - Buy sweets");
// get the users choice
Console.Write("Enter your choice: ");
iChoice = Convert.ToInt32(Console.ReadLine());
//validate user input
while (iChoice < 1 || iChoice > 3)
{
Console.Write("Incorrect option. Please Re-Enter: ");
iChoice = Convert.ToInt32(Console.ReadLine());
}
switch (iChoice)
{
case 1: //user has chosen chocbars
Console.WriteLine();
Console.Write("How many chocbars do you wish to purchase?");
iChocbarsBought = Convert.ToInt32(Console.ReadLine());
iNumChocbars = iNumChocbars - iChocbarsBought;
dTotalMoney = dTotalMoney + (iChocbarsBought * 0.25);
Console.WriteLine("There are now" + iNumChocbars + " chocbars in the machine");
break;
case 2: //User has chosen crisps
Console.WriteLine();
Console.Write("How many crisps do you wish to purchase?");
iCrispsBought = Convert.ToInt32(Console.ReadLine());
iNumCrisps = iNumCrisps - iCrispsBought;
dTotalMoney = dTotalMoney + (iCrispsBought * 0.30);
Console.WriteLine("There are now" + iNumCrisps + " crisps in the machine");
break;
case 3: //user has chosen sweets
Console.WriteLine();
Console.Write("How many sweets do you wish to purchase?");
iSweetsBought = Convert.ToInt32(Console.ReadLine());
iNumSweets = iNumSweets - iSweetsBought;
dTotalMoney = dTotalMoney + (iSweetsBought * 0.20);
Console.WriteLine("There are now " + iNumSweets + " sweets in the machine");
break;
default:
Console.WriteLine("You must enter a number from 1 to 3");
break;
}// end switch
//validate user input
while (iChoice < 1 || iChoice > 3)
{
Console.Write("Incorrect option. Please Re-Enter: ");
iChoice = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("There is now" + dTotalMoney + "p in the machine");
Console.WriteLine();
Console.WriteLine("Press any key to close");
Console.WriteLine();
Console.ReadKey();
}
}
}
}
I guess you are quite new to programming.
What you need to do is place the menu in a function. This is a block of code that you can call from elsewhere in the code. I'm guessing this code comes from the main function?
private static int ShowMenu ()
{
int iChoice = 0;
//display the choices
Console.WriteLine("Vending Machine");
Console.WriteLine("1 - Buy chocbars");
Console.WriteLine("2 - Buy crisps");
Console.WriteLine("3 - Buy sweets");
// get the users choice
Console.Write("Enter your choice: ");
iChoice = Convert.ToInt32(Console.ReadLine());
return iChoice;
}
You would then use this line to show the menu and get the choice
iChoice = ShowMenu();
You could then look at checking that the user's input was a valid number and things like that as part of this function

Not using the else

I'm try to not use the else in the code and just the if. But when I run it and enter "Q" it say error of converting in the total += Convert.ToInt32(input);. Is there a way around this?
do
{
Console.Clear();
Console.WriteLine("Enter a number or Q to quit", input);
input = Console.ReadLine();
if (input.ToUpper() == "Q")
{
Console.WriteLine("Press any key to continue");
Console.ReadKey();
}
else
{
total += Convert.ToInt32(input);
numbersEntered++;
average = ((double)total / numbersEntered);
Console.WriteLine("Total: {0}\t Numbers Entered: {1}\t Average: {2}\t", total, numbersEntered, average);
Console.ReadKey();
}
}
while (input.ToUpper() != "Q");
Try using break; to stop the loop so you don't need the else.
do{
Console.Clear();
Console.WriteLine("Enter a number or Q to quit", input);
input = Console.ReadLine();
if (input.ToUpper() == "Q"){
Console.WriteLine("Press any key to continue");
Console.ReadKey();
break;
}
total += Convert.ToInt32(input);
numbersEntered++;
average = ((double)total / numbersEntered);
Console.WriteLine("Total: {0}\t Numbers Entered: {1}\t Average: {2}\t", total, numbersEntered, average);
Console.ReadKey();
} while (input.ToUpper() != "Q");
Use TryParse to test if the input is a numeric. Here's an example for int (you'll need to define the out variable yourself).
else if (Int32.TryParse(input,out inputVal) {

Categories