0

I am making a bank machine code for a project and whenever you log in it gives an error. The part of the code that is doing it is this:

if(pincheck == pin){
                loggedin = true;
                pincheck = 0;
                do{
                    System.out.println("Welcome, " + name);
                    System.out.println("");
                    System.out.println("Your account balance is $" + balance);
                    System.out.println("");
                    System.out.println("Press 1 to deposit funds");
                    System.out.println("Press 2 to withdraw funds");
                    System.out.println("Press 3 to log out");
                    System.out.println("");
                    options = in.nextInt();

                    switch (options) {
                        case 1: System.out.println("How much would you like to deposit?");          // deposit
                                deposit = in.nextFloat();

                                balance = balance  + deposit;
                                deposit = 0;

                                System.out.println("You have deposited funds into your account.");  // withdraw
                                System.out.println("");
                            break;
                        case 2: System.out.println("How much would you like to withdraw?");
                                withdraw = in.nextFloat();

                                balance = balance - withdraw;
                                withdraw = 0;

                                System.out.println("You have removed funds from your account.");
                                System.out.println("");
                            break;
                        case 3: System.out.println("Logging out...");                               // log out
                                System.out.println("");
                                loggedin = false;
                            break;
                        default:System.out.println("Please enter a valid number");                  // Invalid number
                            break;
                    }
                }while(loggedin = true);

What is happening is to log in you need to put in a number, as pincheck, and if it is equal to the pin that exists it will log you in. I can log in but when I press 3 to log out, it prints logging out and than prints welcome and the whole thing starts again. Can anyone point out where I am getting stuck?

  • 2
    You do know the difference between assignment `=` and comparison for equality `==`? – Some programmer dude Apr 12 '17 at 17:27
  • I am relatively new and with something as complex as this if I just googled what I thought was happening it would tell me things that I already knew. Thanks for the help though, I fixed the code and it is working now! – Christian Brandt Apr 12 '17 at 18:33

1 Answers1

2

= is an assignment operator, so you are simply assigning the value (i.e., setting loggedin=true), which will always be true (because you set to true).

So, you are not checking the actual condition in the loop, so you need to correct the while condition as shown below which uses == operator (used for evaluating the conditional expressions):

while(loggedin == true); //use == for condition evaluation
Vasu
  • 21,832
  • 11
  • 51
  • 67