Change variable in c # statement expression
For a school assignment, I have to create an ATM-like menu view.
My professor gave us this code:
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's 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(" Bank Account");
Console.WriteLine(CurrentBalance);
break;
I've done a little more, but the problem starts here in case "I"
. CustomerName
is replaced with whatever the user enters as expected. But it CurrentBalance
doesn't change and I have to set it equal to something, otherwise I get an error.
I get the feeling that it is impossible to change the variable switch
inside a switch
. I looked into my book for passing references / values, but this section does not contain it switch
. If I could give me a hint what I am doing wrong or can show me what can solve my problem that would be great. I don't expect any code from you, just push a little in the right direction.
source to share
Your problem is your ad placement CurrentBalance
.
You currently have the following:
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 loop do
will not be reset CurrentBalance
until0
source to share
You have to initialize all your variables before entering the loop, not in the loop, otherwise the variable will be reinitialized (cleared before 0
) on each iteration.
double CurrentBalance = 0;
// other code...
do { // ...
I should mention that it has nothing to do with changing variables inside the switch. Changing variables within a switch is perfectly acceptable.
source to share