嘿傢伙所以這是我的程序,我需要通知用戶,如果她/她輸入的信件不是w d b或w,這是一個無效的請求。到目前爲止,我做了什麼,但是當我向dollars_withdraw或dollars_deposit或account_balance輸入數字時,程序將執行該事務,但在返回主循環之前還會添加「無效請求」。我該如何改變它,所以程序不會這樣做的數字輸入的退出存款和餘額?:用戶無效的輸入
// Atm machine.cpp : Defines the entry point for the console application.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char user_request;
string user_string;
double account_balance, dollars_withdraw, dollars_deposit;
account_balance = 5000;
while(account_balance >0)
{
cout << "Would you like to [W]ithdraw, [D]eposit, Check your [b]alance or [Q]uit?"
<< endl;
cin >> user_string;
user_request= user_string[0];
if(user_request == 'w' || user_request== 'W')
{
cout << "How much would you like to withdraw?" << endl;
cin >> dollars_withdraw;
if (dollars_withdraw > account_balance || dollars_withdraw <0)
cout << "Invalid transaction" << endl;
else
account_balance = account_balance - dollars_withdraw;
cout << "Your new balance is $" << account_balance << endl;
}
if (user_request == 'd' || user_request== 'D')
{
cout << "How much would you like to deposit?" << endl;
cin >> dollars_deposit;
if (dollars_deposit <0)
cout << "Invalid transaction" << endl;
else
account_balance= account_balance + dollars_deposit;
cout << "Your new balance is $" << account_balance << endl;
}
if(user_request == 'b' || user_request == 'B')
{
account_balance= account_balance;
cout << "Your available balance is $" << account_balance << endl;
}
if(user_request == 'q' || user_request == 'Q')
break;
else
cout << "Invalid request " << endl;
}
cout << "Goodbye" << endl;
return 0;
}
請重新格式化您的代碼...... – Maxpm 2011-02-02 07:19:32
我的壞,它只是格式化這樣,我不知道爲什麼 – TJL 2011-02-02 07:22:05