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;
}
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.