| | |
Not sure why [continue] is not working
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
Shouldn't continue in my else statement return me to the top do...while loop and print the following again?
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; 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; } } else 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; } else if ((ans == 'b') || (ans == 'B')) cout << "Your balance is: " << balance <<endl; else if ((ans == 'y') || (ans =='Y')) { cout << "\nThank you for banking with Whatever ATM!" << endl; return 0; } else { cout << "\nSorry, input not valid, please select again" << endl; continue; } 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; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
I slept on it and still cant figure it out. Should I have another loop before my if statement? If I do, I don't know what the condition would be. Also for one of the if statements (which i accidentally posted as, I would want it to quit the program, not just break the loop.
C++ Syntax (Toggle Plain Text)
else if ((ans == 'y') || (ans == 'Y'))
•
•
Join Date: Oct 2007
Posts: 12
Reputation:
Solved Threads: 0
Okay, I changed it, adding another loop, but now the continue only goes through once. If the user enters an invalid answer, it does what I want it to, but when i enter another invalid answer, it breaks the loop. Is it something wrong with my condition?
C++ Syntax (Toggle Plain Text)
do-while
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 { 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; 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; break; } } else 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; break; } else if ((ans == 'b') || (ans == 'B')) { cout << "Your balance is: " << balance <<endl; break; } else if ((ans == 'q') || (ans =='q')) break; else { cout << "\nSorry, input not valid, please select again" << endl; 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]; continue; } } 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; }
![]() |
Similar Threads
- Avoid Alienware! (semi OT) (Windows NT / 2000 / XP)
- What's better? Windows 2000 Server or Linux Server? (Windows Servers and IIS)
- nvidia geforce 5600 graphics problem (Monitors, Displays and Video Cards)
- Internet partially stops working after a few hours (Viruses, Spyware and other Nasties)
- IE Will Not Open Anymore (Viruses, Spyware and other Nasties)
- C++ menu problem (C++)
- Cannot get rid of "Cannot Find Server" message (Viruses, Spyware and other Nasties)
- HijackThis log help (Viruses, Spyware and other Nasties)
- How to Download Available Updates (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: Two Player Craps
- Next Thread: Help with pascal triangle output
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






