none
Not sure what I'm doing wrong/how do I create multiple option "trees" RRS feed

  • Question

  • So this is all basically the same root problem, how do I create multiple trees of options from if else statements?

    i.e. I'm trying to make it close if the answer is no on "Anything Else" or "Would you like to withdraw money?". 

    Its throwing an error after string UserInput6 = Console.ReadLine(); and after the curly bracket before the third else if. I've put comments on both locations, both say "} expected"

    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                double TotalAmount;
                TotalAmount = 300.7 + 75.60;
                Console.WriteLine("Your account currently contains this much money: {0:C} ", TotalAmount);
                Console.WriteLine("Would you like to withdraw money? Please type yes or no.");
                string UserInput2 = Console.ReadLine();
                if (UserInput2.ToLower() == "yes") Console.WriteLine("How much would you like to withdraw?");
                {
                    string UserInput = Console.ReadLine();
                    double UserInput3 = double.Parse(UserInput);
                    TotalAmount = TotalAmount - UserInput3;
                    Console.WriteLine("Thank you. Your new balance is {0:C} ", TotalAmount);
                    Console.WriteLine("Anything else?");
                    string UserInput4 = Console.ReadLine();

                    if (UserInput4.ToLower() == "yes") Console.WriteLine("What do you want to do?");
                    Console.WriteLine("1). Withdraw");
                    Console.WriteLine("2). Deposit");
                    Console.WriteLine("Use number key for selection");
                    string UserInput5 = Console.ReadLine();
                    if (UserInput5.ToLower() == "1") Console.WriteLine("How much would you like to withdraw?");
                    else if (UserInput5.ToLower() == "2") Console.WriteLine("How much would you like to deposit?");
                    string UserInput6 = Console.ReadLine(); //HERE
                                
                else if (UserInput4.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
                    {
                        System.Environment.Exit(1);
                    }
                } //HERE TOO
                else if (UserInput2.ToLower() == "no") Console.WriteLine("Have a nice day, and thanks for talking to me. Being an ATM is lonely :'(");
                {
                    System.Environment.Exit(1);
                }
                    
               
              }
        }
    }

    Friday, October 7, 2016 5:36 AM

Answers

  • Hi,

    I see a few issues that I would change.

    a) Your if (UserInput4.ToLower() == "yes") - I think you missed the { } brackets there.

    b) If you have multiple if statements inside each other then code gets hard to read. You should avoid such code which could be easily done with splitting up the code into multiple methods.

    c) What you have there is more or less a state machine. You should define the states exactly and what movements you really want between the states. So you could have these states:

    1) Main Menu (Where the User has to decide what to do)
    2) Deposit Money
    3) Withdraw Money
    4) Anything else? State
    5) Of course the "end"

    The state transitions could be:
    So from 1 you could go to 2 or 3. After 2 or 3 you get to 4. From 4 you get to 5 or back to 1.

    Now you could simply write a method for each state and then call the next method when you need the transition. (Important that you do nothing after such a call!) This is not a real solution because you increase the stack all the time and if your application will not end then you will get a stack overflow or an out or memory exception.

    A better solution could be:
    - each state method returns the next state
    - Your state machine has a state variable which is initialized with the start state (1 in my example)
    - The action of the state machine only has a infinite loop with a switch statement on the state variable with entries like:
    case 1: state = MainMenuState(); break;
    case 2: state = DepositMoneyState(); break;
    and so on.

    We didn't do an object oriented design, but normally that is required. So we would have a ATM (which is the state machine) and some "account". The State would be more complex, because it could contain an account and stuff like that. But to keep it really simple (You only have one account in your code), you could have the loop inside the main method, the balance could be a static variable in the class of the main method and all methods for the states could be simple static methods in the same class.

    I hope this helped. If you was able to follow me, then you should easily finish your code. And the problem you faced would be quite easy t solve because you have one "End" State where you could do the System.Environement.Exit call. (Not nice, instead of calling an EndState method, you could simple leave the loop (so instead of an infinite loop you simply loop till a variable is set to false e.g. a variable continue which is initialized to true. do { ... } while (continue); and then a simple case 5: continue = false; break;

    With kind regards,

    Konrad

    Friday, October 7, 2016 6:16 AM