3

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.

0

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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( ... )
    {

    }
}

Comments

1

You should initialize all your variables before going into the loop, not in the loop, or else the variable is re-initialized (cleared to 0) every iteration.

double CurrentBalance = 0;
// other code...
do { // ...

I should mention it doesn't have anything to do with changing variables within a switch. Changing variables within a switch is perfectly permissible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.