Why does my while loop fall through?

I'm sure it's something simple that I'm just overlooking but I'm going crazy trying to figure out why this isn't working. I'm currently reading through a beginner c++ book and trying to make an ATM program where in a while loop, you can make deposits or withdrawals and the program keeps an updated balance.

This code isn't finished obviously, only the deposit if statement is coded. However, if you choose deposit, and enter a number, it adds the number to the balance, displays the new balance, asks if you want to make another transaction, and then the program ends without getting input or looping back.

However, if you choose "withdraw" which is a valid option, but not coded yet, it passes all the if statements, asks if you want to make another transaction, and does take input and loop back to the menu. This makes me think that I'm looping it back right, but something with the if statement for deposit is doing something strange that I'm not seeing.

Anyways, here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//ATM program

#include <iostream>
#include <string>
using namespace std;

int main() {

   float balance = 0;
   float newBalance = 0;
   float adjustment = 0;
   char response[256];
   string restart;

   cout << "Do you have a transaction to make? : ";
   getline(cin,restart);

   for(int i = 0; i < restart.length(); i++) 
      restart[i] = toupper(restart[i]); 

   while(restart == "YES" | restart == "Y") {
      cout << endl << "What would you like to do? (1 = Deposit , 2 = Withdraw , 3 = Get Balance)> ";
      cin.getline(response,256);
      
      if(strlen(response) == 0) {
         cout << "You must make a selection!" << endl;
         return 1;
      }
      else
      if(atoi(response) < 1 | atoi(response) > 3) {
         cout << response << " - is not a valid selection!" << endl;
         return 1;
      }
      else
      // Deposits
      if (atoi(response) == 1) {
         cout << "How much would you like to deposit?: ";
         cin >> adjustment;
         newBalance = balance + adjustment;
         cout << endl << endl << "Thank you for your deposit!" << endl;
         cout << "Your previous balance was : " << balance << endl;
         cout << "You deposited             : " << adjustment << endl;
         cout << "Your new balance is       : " << newBalance << endl << endl;
      }
  
      cout << "Would you like to make another transaction? : ";
      getline(cin,restart);
      for(int i = 0; i < restart.length(); i++) 
         restart[i] = toupper(restart[i]); 
   }//end of while
   return 0;
}//end of main 


Here is the output to show what I'm talking about:


XXXX@XXXX:~/cpp/prac$ ./bank
Do you have a transaction to make? : yes

What would you like to do? (1 = Deposit , 2 = Withdraw , 3 = Get Balance)> 2
Would you like to make another transaction? : yes

What would you like to do? (1 = Deposit , 2 = Withdraw , 3 = Get Balance)> 1
How much would you like to deposit?: 150


Thank you for your deposit!
Your previous balance was : 0
You deposited             : 150
Your new balance is       : 150

Would you like to make another transaction? : XXXX@XXXX:~/cpp/prac$
Last edited on
I've tried it and I believe it is because you are mixing cin >> and getline() in lines 38 and 47 respectively. I changed line 47 to cin and it looped.

If you wish to retain your getline(), you might have to find another method to get the adjustment. Some great methods are outlined in this thread by Zaita - http://www.cplusplus.com/forum/articles/6046/
hmmm when I replace line 47 with cin >> restart;, it stops to take input, and then loops to the top but then falls through again in a different way. Like this:


XXXX@XXXX:~/cpp/prac$ ./bank
Do you have a transaction to make? : yes

What would you like to do? (1 = Deposit , 2 = Withdraw , 3 = Get Balance)> 1
How much would you like to deposit?: 500


Thank you for your deposit!
Your previous balance was : 0
You deposited             : 500
Your new balance is       : 500

Would you like to make another transaction? : yes

What would you like to do? (1 = Deposit , 2 = Withdraw , 3 = Get Balance)> You must make a selection!
XXXX@XXXX:~/cpp/prac$ 
I believe the problem was in line 16. And I should take a wild guess to believe that the problem..

cin.getline(response, 256)

..is getline again.

It really is advisable to try Zaita's methods. Cin causes quite some unwanted problems (what if someone enters a letter when you want a number?).

For checking out errors, I suggest using a debugger, to step through each line to pin what is going wrong.
Topic archived. No new replies allowed.