Member Avatar for srentrop

The code below is a working program, but I just can't figure out how to correct the loop. I need it to loop back at the complection of case A, B, C and to end with case X.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtmProject
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare an initialize variables
            string sChoice = String.Empty;
            string sResponse = String.Empty;
            Double bBal = 500.00; 
            Double aDeposit = 0.0;
            Double nBal = 0.0;
            Double aWithdraw = 0.0;

            do
            {
                // Show the menu
                ShowMenu();
                // Get the user's choice and store it in a string as an upper case letter
                sChoice = Console.ReadLine().ToUpper();
                // This validates that the user made a correct choice
                while ((sChoice != "A") && (sChoice != "B") && (sChoice != "C") && (sChoice != "X"))
                {
                    Pause("Please enter a choice from the menu!");
                    ShowMenu();
                    //sChoice = Console.ReadLine().ToUpper;
                }
                // Based on the user's choice, switch to the correct segment
                switch (sChoice)
                {
                    // in case they choose A
                    // Ask for an amount
                    // Obtain the amount and store it as a string
                    case "A":
                        Console.Write("\n\tPlease enter the amount to deposit.  ");
                        sResponse = Console.ReadLine();
                        // try to convert the amount to a value of type double and store it
                        try
                        {
                            aDeposit = Convert.ToDouble(sResponse);
                            // Add the amount to the current balance
                            nBal = Convert.ToDouble(bBal + aDeposit);

                            // Tell the user his new balance
                            Console.WriteLine("\n\tYour new balance is {0}", nBal);
                        }
                        catch (Exception ex)
                        {
                            // if there is an error, catch it and close the program
                            Console.WriteLine("\n\t" + ex.Message);
                            Pause("\n\tInvalid amount. This transcation is cancelled...");
                            return;
                        }
                        break;
                    // in case the choose B
                    // Ask for an amount
                    // obtain the amount and store it as an amount
                    case "B":
                        Console.Write("\n\tPlease enter the amount to withdraw.  ");
                        sResponse = Console.ReadLine();
                        // try to convert the amount to a value of type double and store it
                        try
                        {
                            aWithdraw = Convert.ToDouble(sResponse);
                            // subtract the amount from the current balance
                            nBal = (bBal - aWithdraw);
                            // tell the user his new balance
                            Console.WriteLine("\n\tYour new balance is {0}", nBal);
                        }
                        catch (Exception ex)
                        {
                            // if there is an error, catch it and close the program
                            Console.WriteLine("\n\t" + ex.Message);                  
                            Pause("\n\tInvalid amount. This transcation is cancelled...");
                            return;

                        }
                        break;
                    // in case they choose C
                    // just tell the user his balance
                    case "C":
                        Console.WriteLine("\n\tYour current balance is {0}", bBal);
                        break;
                    // in case they choose X
                    // tell the user goodbye
                    case "X":
                        Console.WriteLine("\n\tThanks for banking with us.");
                        Console.WriteLine("\n\tHave a nice day!!");
                        return;

                    default:
                        Pause("Invalid selection. This transcation is cancelled...");
                        return;

                }
            // keep looping while the user's choice isn't X
            } while (sResponse == "A" );
            Pause("\n\tHave a nice Day!!");
        }

        static void Pause(String s)
        {
            Console.WriteLine("\n\t" + s);
            Console.WriteLine("\n\tPress <enter> to Continue.");
            Console.ReadLine();
        }

        static void ShowMenu()
        {
            Console.Clear();
            Console.WriteLine("\n\n\tBanking Application Basic Menu");
            Console.WriteLine("\n\tA) Deposit Funds");
            Console.WriteLine("\tB) Withdraw Funds");
            Console.WriteLine("\tC) Check Balance");
            Console.WriteLine("\n\tX) Exit The System");
            Console.Write("\n\tSelect an option: ");
        }
    }
}

Recommended Answers

All 2 Replies

Your loop needs to do what my snippet does,so I hope it helps.

This will make the do while loop to be looping till the sResponse becomes X

// keep looping while the user's choice isn't X
            } while (sResponse != "X" );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.