### Looping program back to the beginning

I have created a mortgage amorization program for school. After fixing over 100 errors I have got the program to work. However, I have tried to no avail to loop the program back to the beginning using a bool loop, a question, and within the indicator part of the program. The rest of the program works. Can somone look at my code and tell me how to code the program to loop back to the beginning and where the loop needs to be placed, thanks.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177`` `````` #include "math.h" #include #include #include using namespace std; using std::cout; using std::endl; using std::cin; //mortgage class// class Mortgage { public: void header(); void enterprinc(); double princ; double anInt; int yrs; }; //define function// void Mortgage::header() { cout << "Welcome to Smith's Amortization Calculator\n\n"; cout << endl; cout << "Week 3 Individual Assignment C++ Mortgage Amortization Calculator\n\n"; cout << endl; } void Mortgage::enterprinc() { cout << endl; cout << "Enter the amount The Mortgage Amount:\$"; cin >> princ; } //main function// int main () { Mortgage mortgageTotal; double princ = 0; double anInt [4]= {0, 5.35, 5.5, 5.75,}; double yrs [4]= {0, 7, 15, 30,}; double pmt = 0; double intPd = 0; double bal = 0; double amtPd = 0; double check(int min, int max); int NOP = 0; int Mnth = 0; int SL = 0; char indicator = ('A' ,'a','R','r','D','d'); int select; //Begin Loop { mortgageTotal.header (); mortgageTotal.enterprinc (); cout<< "Please Enter Your Selection!"<< endl; cout<< "Press A to make selection for term of mortgage and interest rate"<> indicator; if (indicator == 'A','a') { cout << "Please select your Terms and Rates from the following choices: " << endl; cout << "1. 7 years at 5.35%" << endl; cout << "2. 15 years at 5.5%" << endl; cout << "3. 30 years at 5.75%" << endl; cout << endl; cout << "What is your selection:"; select = 0; cin >> select; cout << endl << endl << endl; switch (select) { case 0: cout << endl; break; case 1: yrs[2] = 7; anInt[2] = 5.35; cout << "You have selected a 7 year mortgage at 5.35% interest." << endl; break; case 2: yrs[3] = 15; anInt[3]= 5.50; cout << "You have selected 15 year mortgage at 5.50 interest%" << endl; break; case 3: yrs[4] = 30; anInt[4] = 5.75; cout << "You have selected a 30 year mortgage at 5.75% interest" << endl; break; default: cout << "Invalid Line Number" << endl; if ((select < 1)|| (select > 3)) { system("cls"); cout << "Your choice is invalid, please enter a valid choice!!!"; system("PAUSE"); system("cls"); return main(); } } } //Formulas// double pmt = (mortgageTotal.princ*((anInt[select]/1200)/(1 - pow((1+(anInt[select]/1200)),-1*(yrs[select]*12))))); // notice the variable in the brackets that i added. cout << "Your Monthly Payment is: \$" << pmt << "\n"; cout << endl; double NOP = yrs [select] * 12; SL = 0; for (Mnth = 1; Mnth <= NOP; ++Mnth) { intPd = mortgageTotal.princ * (anInt[select] / 1200); amtPd = pmt - intPd; bal = mortgageTotal.princ - amtPd; if (bal < 0)bal = 0; mortgageTotal.princ = bal; if (SL == 0) { cout << "Balance of Loan" << "\t\t\tAmount of Interest Paid" << endl; } cout << setprecision(2) << fixed << "\$" << setw(5) << bal << "\t\t\t\t\$"<< setw(5) << intPd << endl; ++SL; //Allows user to decide whether or not to continue// if (SL == 12) { cout << "Would you like to continue the amoritization or quit the program?\n"; cout << endl; cout << "Enter 'R' to see the remainder or 'D' for done.\n"; cin >> indicator; if (indicator == 'R','r')SL = 0; else if (indicator == 'D','d') cout << endl; } } } return 0; } ``````
I'd use a do..while loop.
 ``123456789101112131415161718192021`` ``````#include #include using namespace std; int main() { bool done = false; do { if (tolower( indicator ) == 'd') { cout << endl; done = true; } } while (!done); cout << "Good bye.\n"; return 0; }``````

Hope this helps.
Thanks for your tip. Your example gave me an idea. I added a bool statement to the loop and added some new lines of code and deleted some existing lines of code. I am posting the completed program that meets the programs requirments because I struggles so much with this assignment that I thought maybe my example would help another student with ideas. When I first started I had over 100 errors and then I could not loop the program back to the beginning, but after several days and a lot of stress, I finally obtained my goal. Thank you very much.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184`` `````` #include "math.h" #include #include #include using namespace std; using std::cout; using std::endl; using std::cin; //mortgage class// class Mortgage { public: void header(); void enterprinc(); double princ; double anInt; int yrs; }; //define function// void Mortgage::header() { cout << "Welcome to Smith's Amortization Calculator\n\n"; cout << endl; cout << "Week 3 Individual Assignment C++ Mortgage Amortization Calculator\n\n"; cout << endl; } void Mortgage::enterprinc() { cout << endl; cout << "Enter the amount The Mortgage Amount:\$"; cin >> princ; } //main function// int main () { Mortgage mortgageTotal; double princ = 0; double anInt [4]= {0, 5.35, 5.5, 5.75,}; double yrs [4]= {0, 7, 15, 30,}; double pmt = 0; double intPd = 0; double bal = 0; double amtPd = 0; double check(int min, int max); int NOP = 0; int Mnth = 0; int SL = 0; char indicator = ('A' ,'a','R','r','D','d'); int select; //Begin Loop bool finished = false; while( !finished ) { mortgageTotal.header (); mortgageTotal.enterprinc (); cout<< "Press 'A' to make selection for term of mortgage and interest rate"<> indicator; if (indicator == 'A','a') { cout << "Please select your Terms and Rates from the following choices: " << endl; cout << "1. 7 years at 5.35%" << endl; cout << "2. 15 years at 5.5%" << endl; cout << "3. 30 years at 5.75%" << endl; cout << endl; cout << "What is your selection:"; select = 0; cin >> select; cout << endl << endl << endl; switch (select) { case 0: cout << endl; break; case 1: yrs[2] = 7; anInt[2] = 5.35; cout << "You have selected a 7 year mortgage at 5.35% interest." << endl; break; case 2: yrs[3] = 15; anInt[3]= 5.50; cout << "You have selected 15 year mortgage at 5.50 interest%" << endl; break; case 3: yrs[4] = 30; anInt[4] = 5.75; cout << "You have selected a 30 year mortgage at 5.75% interest" << endl; break; default: cout << "Invalid Line Number" << endl; if ((select < 1)|| (select > 3)) { system("cls"); cout << "Your choice is invalid, please enter a valid choice!!!"; system("PAUSE"); system("cls"); return main(); } } } //Formulas// double pmt = (mortgageTotal.princ*((anInt[select]/1200)/(1 - pow((1+(anInt[select]/1200)),-1*(yrs[select]*12))))); // notice the variable in the brackets that i added. cout << "Your Monthly Payment is: \$" << pmt << "\n"; cout << endl; double NOP = yrs [select] * 12; SL = 0; for (Mnth = 1; Mnth <= NOP; ++Mnth) { intPd = mortgageTotal.princ * (anInt[select] / 1200); amtPd = pmt - intPd; bal = mortgageTotal.princ - amtPd; if (bal < 0)bal = 0; mortgageTotal.princ = bal; if (SL == 0) { cout << "Balance of Loan" << "\t\t\tAmount of Interest Paid" << endl; } cout << setprecision(2) << fixed << "\$" << setw(5) << bal << "\t\t\t\t\$"<< setw(5) << intPd << endl; ++SL; //Allows user to continue amortization// if (SL == 12) { cout << "Enter 'R' to see the remainder or 'D' when mortgage amortization is finished.\n"; cin >> indicator; if (indicator == 'R','r')SL = 0; else if (indicator == 'D','d') cout << endl; } } //loops program back to the beginning// char tempChar; cout << "Do you wish to loop through the code again? [Y/N]: "; cin >> tempChar; if( tempChar == 'N' || tempChar == 'n' ) finished = true; } return 0; } ``````
Topic archived. No new replies allowed.