2011-02-02 134 views
0

嘿傢伙所以這是我的程序,我需要通知用戶,如果她/她輸入的信件不是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; 
} 
+2

請重新格式化您的代碼...... – Maxpm 2011-02-02 07:19:32

+0

我的壞,它只是格式化這樣,我不知道爲什麼 – TJL 2011-02-02 07:22:05

回答

2

當然可以。您的代碼表示:

  • 如果它是一個 'W' 做點什麼
  • ...
  • 如果它是一個 'Q' 做的事,其他大喊 「無效」

所以如果用戶沒有輸入'q',最後的'else'塊將會被執行。要麼使用else if整個或更改代碼以使用switch聲明:

// Either: 
if (user_request == ...) { 
    ... 
} else if (user_request == ...) { 
    ... 
} else { 
    std::cout << "invalid"; 
} 

// Or (better, faster): 
switch (user_request) { 
case 'q': 
case 'Q': 
    ... 
    break; 
... 
default: 
    std::cout << "Invalid request"; 
} 

第三種選擇是使用continue

while (...) { 
    user_request = ... 
    if (user_request == 'w' ...) { 
     ... 
     continue; // In this iteration, no other code within the while loop is executed. 
    } 
    if (...) 
    ... 
} 
1

鏈中的if語句到如果,否則,如果else-如果別的。

0

在我的老CS類,我會做這樣的事情:

string user_string; 
do { 
    if(user_string) cout << "Enter a valid value!" << endl; 
    cin >> user_string; 
} while(user_string != "w" && user_string != "d"); 
2

這是一個不好的編程習慣。請使用Switch Case來滿足您的需求。並且在每個案例分支之後放置一個「break」聲明。

0

您需要使用else if如下:只有

if(user_request == 'w' || user_request== 'W') 
{ 
    ... 
} else if(user_request == 'd' || user_request== 'D') 
{ 
    .... 
} else if(user_request == 'b' || user_request== 'B') 
{ 
    ..... 
} else if(user_request == 'q' || user_request== 'Q') 
{ 
    ... 
} else 
{ 
    // Invalid request 
} 
1

else聲明 「知道」 的if聲明立即前面。例如:

if (myNumber == 0) 
{ 
    // Triggers when myNumber is zero. 
} 

if (myNumber == 1) 
{ 
    // Triggers when myNumber is one. 
} 

else 
{ 
    // Triggers when myNumber is not one. 
} 

這可以用else if聲明修復。在你的情況下,它會是這個樣子:

if (user_request == w) 
{ 
    // ... 
} 

else if (user_request == d) 
{ 
    // ... 
} 

// ... 

else cout << "Invalid request.";