943,782 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1179
  • C++ RSS
Oct 27th, 2007
0

Simple ATM, just need to add one more thing

Expand Post »
Hello. I wrote a program which works fine, all i need is to add one more thing. When I ask the user to input an answer (withdraw, balance...) I don't know how to prompt them if they enter something else, such as "golfball" and loop them back to entering the correct one.

Here is what I have:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. double withdraw, deposit, balance, newBalance, amount;
  8. char ans;
  9. string choice;
  10.  
  11. balance = 1000;
  12.  
  13. cout << "Welcome to Whatever ATM!" << endl;
  14.  
  15. do
  16. {
  17. cout << "\nWhat would you like to do from the following?\n"
  18. << "\nWithdraw from account\n"
  19. << "Deposit money into account\n"
  20. << "Balance inquiry\n"
  21. << "Quit\n"
  22. << "\nPlease select now" << endl;
  23.  
  24. cin >> choice;
  25. ans = choice[0];
  26. newBalance = balance;
  27.  
  28.  
  29.  
  30. do
  31. {
  32. if ((ans == 'w') || (ans == 'W'))
  33. {
  34. cout << "\nWhat amount would you like to withdraw?" << endl;
  35. cin >> amount;
  36.  
  37. while (amount > balance)
  38. {
  39. cout << "Sorry, you have insufficient funds.\n"
  40. << "Please try again: ";
  41. cin >> amount;
  42. }
  43.  
  44. if (amount <= balance)
  45. {
  46. balance = balance - amount;
  47. newBalance = balance;
  48. cout << "Thank you, your new balance is: " << newBalance << endl;
  49. }
  50. }
  51.  
  52. if ((ans == 'd') || (ans == 'D'))
  53. {
  54. cout << "\nWhat amount would you like to deposit?" << endl;
  55. cin >> amount;
  56. balance = balance + amount;
  57. newBalance = balance;
  58. cout << "Thank you, your new balance is: " << newBalance <<endl;
  59. }
  60.  
  61. if ((ans == 'b') || (ans == 'B'))
  62. cout << "Your balance is: " << balance <<endl;
  63.  
  64. if ((ans == 'q') || (ans == 'Q'))
  65. {
  66. cout << "\nThank you for banking with Whatever ATM!" << endl;
  67. return 0;
  68. }
  69.  
  70. }
  71. while ((ans == 'w') && (ans == 'W') && (ans == 'd') && (ans == 'D')
  72. && (ans == 'b') && (ans == 'B') && (ans == 'q') && (ans == 'Q'));
  73.  
  74.  
  75. cout << "\nWould you like to make another transaction? (Y/N) ";
  76. cin >> choice;
  77. ans = choice[0];
  78. }
  79. while ((ans == 'y') || (ans =='Y'));
  80.  
  81. cout << "\nThank you for banking with Whatever ATM!" << endl;
  82. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007
Oct 27th, 2007
0

Re: Simple ATM, just need to add one more thing

Data validation isn't an easy process, but it isn't rocket science either. There are two common approaches.

The first is to never accept input as any type other than as a string because strings are always valid. Doing this you would then validate that all the characters in the string are valid for the type you want and then convert the string to that type.

The second way is to check if the stream goes into a failed state or not when the user enters input. If it does then you need to clear the input buffer and reset/clear the input stream.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Oct 27th, 2007
0

Re: Simple ATM, just need to add one more thing

I'm still confused.
First of all, for a beginner, are the ways I set up my loops okay?.
Second, how and where do I check to see if the input is valid? I don't know what you mean about "input buffer" and "reset/clear the input stream"
And lastly, am I not reading them in as strings?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dabu is offline Offline
12 posts
since Oct 2007
Oct 27th, 2007
0

Re: Simple ATM, just need to add one more thing

Well, a student project but at least there was an effort to solve that.

#include <iostream>
#include <string>

 
int main()
{ 
   double withdraw, deposit, balance, newBalance, amount;
   char ans;
   string choice;

   balance = 1000;
    
    bool good = false;
   cout << "Welcome to Whatever ATM!" << endl;

   do
   {
        while(!good){

            cout << "\nWhat would you like to do from the following?\n"
            << "\nWithdraw from account\n"
            << "Deposit money into account\n"
            << "Balance inquiry\n"
            << "Quit\n"
            << "\nPlease select now" << endl;

            cin >> choice; 
            ans = choice[0];
            switch(ans)
            {
    case 'w':
    good = true;
    break;
    /*go on with your cases
        to cover all your  allowed values*/
            }
         }
       newBalance = balance;


              
       do
       {
           if ((ans == 'w') || (ans == 'W'))
           {
               cout << "\nWhat amount would you like to withdraw?" << endl;
               cin >> amount;
               
               while (amount > balance)
               {
                   cout << "Sorry, you have insufficient funds.\n"
                   << "Please try again:  ";
                   cin >> amount;
               }
               
               if (amount <= balance)
               {
                   balance = balance - amount;
                   newBalance = balance;
                   cout << "Thank you, your new balance is: " << newBalance << endl;
               }
           }
           
           if ((ans == 'd') || (ans == 'D'))
           {
               cout << "\nWhat amount would you like to deposit?" << endl;
               cin >> amount;
               balance = balance + amount;
               newBalance = balance;
               cout << "Thank you, your new balance is: " << newBalance <<endl;
           }
           
           if ((ans == 'b') || (ans == 'B'))
               cout << "Your balance is:  " << balance <<endl;
           
           if ((ans == 'q') || (ans == 'Q'))
           {
               cout << "\nThank you for banking with Whatever ATM!" << endl;
               return 0;
           }
       
      }
      while ((ans == 'w') && (ans == 'W') && (ans == 'd') && (ans == 'D')
          && (ans == 'b') && (ans == 'B') && (ans == 'q') && (ans == 'Q')); 


       cout << "\nWould you like to make another transaction? (Y/N)  ";
       cin >> choice;
       ans = choice[0];
   }
   while ((ans == 'y') || (ans =='Y'));

   cout << "\nThank you for banking with Whatever ATM!" << endl;
}
Reputation Points: 12
Solved Threads: 1
Newbie Poster
tostrinj is offline Offline
20 posts
since Jul 2007
Oct 27th, 2007
0

Re: Simple ATM, just need to add one more thing

I don't see why you need the inner do/while loop. The logic for the while condition of the inner while loop is wrong since answer can only have one value at a time and using the logic you have will only be true if ans has more than one value at a time.

choice is a string but amount is a double, though now that I reread your origninal post you probably aren't looking for data validation. sorry.

It looks like what you want to do is to use a default. The routine for that is something like this;
C++ Syntax (Toggle Plain Text)
  1. int choice;
  2. cout << "select one of the following using the number of the line listed" << endl;
  3. cout << "1) withdrawal" << endl;
  4. cout << "2) deposit" << endl;
  5. cin >> choice;
  6. do
  7. {
  8. if(choice == 1)
  9. //do this
  10. else if(choice == 2)
  11. //do that
  12. else //this is the default to catch any input thats wrong
  13. //do something else
  14.  
  15. cout << "go again y/n" << endl;
  16. char s;
  17. cin >> s;
  18. }while(s == 'y')
Often, instead of a series of if/else if/else statements a switch statement is used like thisl
C++ Syntax (Toggle Plain Text)
  1. switch(choice)
  2. {
  3. case 1:
  4. //do this
  5. break;
  6. case 2:
  7. //do that
  8. break;
  9. default:
  10. //do something else
  11. break;
  12. }

but if you've never heard of switch statements before, then the sequential if/else statement work, too.

Oops, I see tostrinj already posted the switch option.
Last edited by Lerner; Oct 27th, 2007 at 9:15 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Puzzled with reading multiple lines from file
Next Thread in C++ Forum Timeline: Regex





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC