"if" statement outside while loop does not work

Cannot get the last "if" on line 52 outside the while loop to work and print out the message. When I enter "q" or "Q" the message for the last "if" does not print. The program mimics an automated teller machine at the bank.

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
53
54
55
56
57
58
59
#include <iostream> 
#include <string> 
using namespace std;

int main()
{
  string choice;
  float deposit, withdraw;
  float balance = 5000.00;
  cout<<"Hi. You have five thousand bucks in your account. Enter (D) or (d) to  deposit, (W) or (w) to  withdraw, (B) or (b) to view your present balance and (Q) or (q) to exit."<<endl;
  cin>>choice;
  
  
   
  while (choice != "Q" || choice != "q") //while the choice isn't "quit"
  {     
        if (choice=="D" || choice=="d") //if choice is for deposit
          {
           cout<<"Please enter amount to deposit."<<endl; 
           cin>>deposit; 
           if ( (deposit<= -1) || (deposit==0) ) //cannot deposit negative/zero dollars
            {
            cout<<"Error. Cannot deposit negative or zero amount. Please try again."<<endl; 
            }
           else
            balance = balance + deposit; //customer balance added to current balance
            cout<<"Your current balance is "<<balance<<". Here are your options: Enter (D) or (d) to deposit, (W) or (w) to withdraw, (B) or (b) to see present balance and (Q) or (q) to exit."<<endl;
            cin>>choice; 
           }
        if (choice=="W" || choice=="w") //if choice is withdraw
           {
            cout<<"Please enter amount to withdraw."<<endl;
            cin>>withdraw;
            if ( (withdraw > balance) || (withdraw <= 0) ) //cannot withdraw negative or zero dollars
             {
             cout<<"Error. Withdraw amount was either negative or exceeded current balance of "<<balance<<". Please try again."<<endl;
             }
            else
             balance = balance - withdraw; //customer withdraw amount subtracted from current balance
             cout<<"Your current balance is "<<balance<<". Here are your options: Enter (D) or (d) to make a  deposit, (W) or (w) to withdraw, (B) or (b) to see present balance and (Q) or (q) to leave."<<endl;
             cin>>choice; 
           }
        if (choice=="B" || choice=="b") //if choice is "B"/"b" then show present balance 
           {
            cout<<"Your current balance is "<<balance<<". Here are your options: Enter (D) or (d) to deposit, (W) or (w) to withdraw, (B) or (b) to see present balance and (Q) or (q) to exit."<<endl;
            cin>>choice; 
           }
   } // end while loop

   if (choice== "Q" || choice== "q")
   {
    cout<<" Have a nice day."<<endl; //Can't get this to print out
   }
  
  
       
  system ("PAUSE"); 
  return 0; 
}


Thanks.
Last edited on
hum. I try to run it and I have the same problem. I couldn't fingure it out. ^_^...I guess my only advice is to simplify your code a little. And don't need to use string if all that require is a char. Take a look at this to see if it would help. All that if and else is a real mess. ^_^

http://www.cplusplus.com/forum/beginner/6463/

Lines 47 and 48 seem to fix it
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
53
54
55
56
#include <iostream> 
using namespace std;

int main()
{
  char choice;
  float deposit, withdraw;
  float balance = 5000.00;
  cout<<"Hi. You have five thousand bucks in your account. Enter (D) or (d) to  deposit, (W) or (w) to  withdraw, (B) or (b) to view your present balance and (Q) or (q) to exit."<<endl;
  cin>>choice;
  
  
   
  while (choice != 'Q' || choice != 'q') //while the choice isn't "quit"
  {     
        if (choice=='D' || choice=='d') //if choice is for deposit
          {
           cout<<"Please enter amount to deposit."<<endl; 
           cin>>deposit; 
           if (deposit <= 0) //cannot deposit negative/zero dollars
            {
            cout<<"Error. Cannot deposit negative or zero amount. Please try again."<<endl; 
            }
           else
            balance = balance + deposit; //customer balance added to current balance
            cout<<"Your current balance is "<<balance<<". Here are your options: Enter (D) or (d) to deposit, (W) or (w) to withdraw, (B) or (b) to see present balance and (Q) or (q) to exit."<<endl;
            cin>>choice; 
           }
        if (choice=='W' || choice=='w') //if choice is withdraw
           {
            cout<<"Please enter amount to withdraw."<<endl;
            cin>>withdraw;
            if ( (withdraw > balance) || (withdraw <= 0) ) //cannot withdraw negative or zero dollars
             {
             cout<<"Error. Withdraw amount was either negative or exceeded current balance of "<<balance<<". Please try again."<<endl;
             }
            else
             balance = balance - withdraw; //customer withdraw amount subtracted from current balance
             cout<<"Your current balance is "<<balance<<". Here are your options: Enter (D) or (d) to make a  deposit, (W) or (w) to withdraw, (B) or (b) to see present balance and (Q) or (q) to leave."<<endl;
             cin>>choice; 
           }
        if (choice=='B' || choice=='b') //if choice is "B"/"b" then show present balance 
           {
            cout<<"Your current balance is "<<balance<<". Here are your options: Enter (D) or (d) to deposit, (W) or (w) to withdraw, (B) or (b) to see present balance and (Q) or (q) to exit."<<endl;
            cin>>choice; 
           }
        if (choice=='Q' || choice=='q')
        break;
   }
    cout<<" Have a nice day."<<endl; //Can't get this to print out
  
  
       
  system ("PAUSE"); 
  return 0; 
}
The real problem is that the while test is incorrect.
This will always test true so the loop will always run (infinite loop).

It should be while ( choice != 'Q' && choice != 'q') - then the
if check added by gordonfreeman can be removed.
Thanks guys for the replies. I got it to work now. Thanks again for the needed assistance/input.
Topic archived. No new replies allowed.