I was attempting to initilize bool done to false, then have this program continue to give the user the option to do more check balance functions util they pressed exit, however, my program continues to cycle repeatedly, even after option 5 (EXIT) is entered. Please, some help on how to recode so that program breaks out after this selection. Thanks. Will
notdumb 0 Newbie Poster
// Will Urban CS 161 2/11/05
// Assignment 2
#include <iostream>
#include <iomanip>
using namespace::std;
//declare function
void display_menu ();
void get_balances (double& checking, double& savings);
//process functions
void transfer (double& checking, double& savings);
void withdraw (double& checking);
void deposit (double& checking);
void display_balances (double& checking, double& savings);
enum {TRANSFER = 1, WITHDRAW, CHECK, DEPOSIT, EXIT};
//double transfer_amount;
//double amount;
//double amt;
//main function
int main ()
{
double checking, savings;
int choice;
get_balances (checking, savings);
bool done = false;
do{
display_menu ();
cout << "Enter Choice: ";
cin >> choice;
switch (choice)
{
case TRANSFER:
transfer (checking, savings);
break;
case WITHDRAW:
withdraw (checking);
break;
case CHECK:
withdraw (checking);
break;
case DEPOSIT:
deposit (checking);
break;
case EXIT:
(done = true);
break;
}
display_balances (checking, savings);
} while (done = (!false));
return 0;
}
//function definitions
void get_balances ( double& checking, double& savings)
{
do
{
cout << "Enter checking balance: ";
cin >> checking;
} while (checking < 0 );
do
{
cout << "Enter savings balance: ";
cin >> savings;
} while (savings < 0 );
}
void display_menu ()
{
cout << "Banking options " << endl
<< "1) Transfer " << endl
<< "2) Withdawl " << endl
<< "3) Check written " << endl
<< "4) Deposit " << endl
<< "5) Exit " << endl;
}
//process functions
void transfer (double& checking, double& savings)
{
double transfer_amount;
do {
cout << "Enter transfer amount: ";
cin >> transfer_amount;
checking -= transfer_amount;
savings += transfer_amount;
} while ((transfer_amount > checking));
}
void withdraw (double& checking)
{
double amount;
do {
cout << "Enter amount to withdraw/check value: ";
cin >> amount;
checking -= amount;
} while (amount > checking);
}
void deposit (double& checking)
{
double amt;
do {
cout << "Enter amount to deposit: ";
cin >> amt;
checking += amt;
} while (amt < 0);
}
void display_balances (double& checking, double& savings)
{
cout.setf(ios::fixed | ios::showpoint);
cout.precision (2);
cout << setw (10) << "Check balance: "
<< setw (10) << checking
<< setw (10) << " Savings balance: "
<< setw (10) << savings << endl << endl;
} Dani AI
Generated
Two short possibilities explain why the program keeps cycling after you enter the EXIT choice: the loop condition and the flag value are inverted, or input handling never updates choice because cin failed. ’s suggestion to flip the loop logic (or to initialize the boolean so the loop condition matches the flag) is on the right track, but it helps to look at a few other failure modes before changing structure.
Remember these concrete rules while debugging:
- A
breakinside aswitchexits only theswitch, not the outer loop. To stop the loop you must set the loop-control flag (orreturn/break the loop directly) when EXIT is chosen. - If you use a
do...while, make sure the initial boolean and thewhile(...)test agree (one common pattern is “run while not done” and setdone = trueon EXIT). - If
cin >> choicefails (user types non-numeric text),choicewill not change and the loop can repeat forever. Clear the error and discard the bad input before the next read.
Practical checks and a small input-recovery pattern to add:
- Print
choiceand the flag after each loop iteration to watch what values change. - Confirm your EXIT constant really equals the number the menu prints (5).
- Fix obvious copy/paste mistakes in the switch (for example, make sure the CHECK case calls the check-balance routine, not withdraw).
- Use a simple input-recovery block like:
if (!(cin >> choice)) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); continue; // prompt again }
Checklist: align flag initialization with the loop condition, set the flag inside the EXIT case, validate and clear input, and add temporary debug prints. Those four steps will reveal whether the problem is logic inversion, input failure, or a typo in the switch.
odee 0 Light Poster
change do while into while... and initialize done = true instead of false. then when you press 5 (exit) change done = false to exit the loop...
int main ()
{
double checking, savings;
int choice;
get_balances (checking, savings);
bool done = true;
while(done) {
display_menu ();
cout << "Enter Choice: ";
cin >> choice;
switch (choice)
{
case TRANSFER:
transfer (checking, savings);
break;
case WITHDRAW:
withdraw (checking);
break;
case CHECK:
withdraw (checking);
break;
case DEPOSIT:
deposit (checking);
break;
case EXIT:
done = false;
break;
}
display_balances (checking, savings);
}
return 0;
} odee 0 Light Poster
or if you still want your orignal do while loop, change the condition inside the while... like this one:
do {
// statement
} while(done==false); notdumb 0 Newbie Poster
Thanks so much. It is awsome of people to give there time to coach learners on here. Waiting for replies from teachers can take days, as can staring at what tends to be simple problems hoping for a solution to jump onto the screen. I appriciate the help.--Will
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.