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.
#include <iostream>
#include <string>
usingnamespace 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;
}
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. ^_^
#include <iostream>
usingnamespace 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;
}