For a school assignment I'm supposed to create a menu kind of like an ATM.
My professor gave us this code to use:
string choice = null;
do
{
Console.Write("[O]pen Account [I]nquire [D]eposit [W]ithdraw [Q]uit: ");
choice = Console.ReadLine();
choice = choice.ToUpper();
switch (choice)
{
case "O": // open an account
case "I": // inquire
case "D": // deposit
case "W": // withdraw
default: break;
}
} while (choice != "Q");
Here is what I did:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string choice = null;
string CustomerName;
Console.WriteLine("Welcome to Fantasy Bank");
Console.Write("Please enter your name:");
CustomerName = Console.ReadLine();
do
{
Console.WriteLine("What can I do for you");
Console.Write("[O]pen Account [I]nquire [D]eposit [W]ithdraw [Q]uit: ");
choice = Console.ReadLine();
choice = choice.ToUpper();
double CurrentBalance = 0;
switch (choice)
{
case "O": // open an account
Console.Write("Name of account holder:");
Console.WriteLine(CustomerName);
Console.Write("Initial Deposit:");
CurrentBalance = Convert.ToDouble(Console.ReadLine()); // i get a major error if someone types in a letter instead of a number
Console.Write("You have succesfully opened an account with an initial deposit of ");
Console.Write(CurrentBalance);
Console.WriteLine(" at an imaginary bank. Congratulations");
break;
case "I": // inquire
Console.Write(CustomerName);
Console.WriteLine("'s Bank Account");
Console.WriteLine(CurrentBalance);
break;
I did a little bit more, but the problem starts here in case "I". CustomerName is getting replaced by what the user types, like it's supposed to be. But CurrentBalance does not change, and I have to set it equal to something otherwise I get an error.
I start to get the feeling that it may be impossible to change a switch variable inside a switch. I looked in my book for passing references/values, but it doesn't include switch in that section.
If y'all could give me a hint what I'm doing wrong or could show me what could fix my problem, that would be great. I'm not expecting code from you, just a little push in the right direction.
Your problem is the placement of your declaration of CurrentBalance.
Currently you have this:
do
{
double CurrentBalance = 0;
switch (choice) {
/* the rest of your code */
}
}
Should be
double CurrentBalance = 0;
do
{
switch (choice) {
/* the rest of your code */
}
}
Now, the next iteration of your do loop does not reset CurrentBalance to 0
Every iteration of the loop you reset CurrentBalance to 0. Move the line double CurrentBalance = 0; :
string choice;
string CurrentName;
double CurrentBalance = 0;
// ...
do
{
// ...
//double CurrentBalance = 0; NOT HERE
switch( ... )
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With