invalid input by the user

Posted by TJL on Stack Overflow See other posts from Stack Overflow or by TJL
Published on 2011-02-02T07:17:36Z Indexed on 2011/02/02 7:25 UTC
Read the original article Hit count: 175

Filed under:

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

© Stack Overflow or respective owner

Related posts about c++