用户输入无效

时间:2011-02-02 07:17:36

标签: c++

嘿家伙所以这是我的程序,我需要通知用户,如果她/她输入的字母不是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;
}

6 个答案:

答案 0 :(得分: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 :(得分:2)

这是一个糟糕的编程习惯。请根据需要使用Switch Case。并在每个案例分支后加上“中断”声明。

答案 2 :(得分:1)

将if语句链接到if,else-if,else-if,...,else。

答案 3 :(得分: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.";

答案 4 :(得分: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");

答案 5 :(得分: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
}