Simple ATM, just need to add one more thing

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

Simple ATM, just need to add one more thing

 
0
  #1
Oct 27th, 2007
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #2
Oct 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 12
Reputation: dabu is an unknown quantity at this point 
Solved Threads: 0
dabu dabu is offline Offline
Newbie Poster

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

 
0
  #3
Oct 27th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: tostrinj is an unknown quantity at this point 
Solved Threads: 1
tostrinj's Avatar
tostrinj tostrinj is offline Offline
Newbie Poster

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

 
0
  #4
Oct 27th, 2007
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;
}
===========================
can you repeat the part of the stuff where you said all about the things?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #5
Oct 27th, 2007
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;
  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
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC