| | |
Simple ATM, just need to add one more thing
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
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:
Here is what I have:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main() { double withdraw, deposit, balance, newBalance, amount; char ans; string choice; balance = 1000; cout << "Welcome to Whatever ATM!" << endl; do { 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]; 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; }
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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.
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.
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?
can you repeat the part of the stuff where you said all about the things?
•
•
Join Date: Jul 2005
Posts: 1,681
Reputation:
Solved Threads: 264
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;
Often, instead of a series of if/else if/else statements a switch statement is used like thisl
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.
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)
int choice; cout << "select one of the following using the number of the line listed" << endl; cout << "1) withdrawal" << endl; cout << "2) deposit" << endl; cin >> choice; do { if(choice == 1) //do this else if(choice == 2) //do that else //this is the default to catch any input thats wrong //do something else cout << "go again y/n" << endl; char s; cin >> s; }while(s == 'y')
C++ Syntax (Toggle Plain Text)
switch(choice) { case 1: //do this break; case 2: //do that break; default: //do something else break; }
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.
![]() |
Similar Threads
- ATM program part 2 (Python)
- Quick Question: Is J# the same thing as Java? (Java)
- web input (IT Professionals' Lounge)
- code illiterate - add text from 5 TEdit to 1 (Pascal and Delphi)
- Really simple thing... (C)
- My list box (Java)
- Add Sound To Cd Burner (Windows NT / 2000 / XP)
- C++ Help (C++)
- Mysearch toolbar, please help !! (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Puzzled with reading multiple lines from file
- Next Thread: Regex
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






