开发者

invalid input by the user

开发者 https://www.devze.com 2023-02-08 05:22 出处:网络
hey guys so this is my program, I need to notify the user that if hhe/she enters a letter other than w d b or w that is an invalid request. what ive done so far does this, but when i input a number to

hey guys so this is my program, I need to notify the user that if hhe/she enters a letter other than w d b or w that is an invalid request. what ive done so far does this, but when i input a number to the dollars_withdraw or dollars_deposit or account_balance the program will do the transaction 开发者_Python百科but also add the "invalid request" before going back to main loop. how do i change it so the program wont do that for numerical inputs for the withdraw deposit and 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;
}


Sure it does. Your code says:

  • If it is a 'w' do something
  • ...
  • If it is a 'q' do something, else yell "invalid"

So if the user does not enter a 'q', the last 'else' block will always be executed. Either use else if throughout or change your code to use a switch statement:

// 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";
}

A third option would be to use continue:

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


This is a bad programming practice. Please use Switch Case for what you need to achieve. And put a "break" statement after every case branch.


chain your if statements into if, else-if, else-if, ..., else.


else statements only "know of" the if statement immediately previous. For example:

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

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

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

This can be fixed with else if statements. In your case it would look something like this:

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

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

// ...

else cout << "Invalid request.";


In my old CS class, I'd do things like this:

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


You need to use else if as follows:

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消