446,332 Members | 1,427 Online Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

# Loops to Allow users to go back and enter new data or just quit

 P: 10 I am working on an assignment where I have to allow end-users to enter in data and to have C++ make a calculation. From there I have to set up a loop to allow users to go back and enter new data or just quit. I have the part down where the users are entering in information to get a calculation, but I am not understanding the part about looping to go back, any direction will be greatly appreciated. Here is my code for now: //************************************************** ***** //Program: Calculations Payments //Purpose: To calculate a mortgage monthly payment by the user entering input // and to allow the user to enter new data if they wish or to just quit the program. //Programmer: Cameron Faldeti //Date: 10/30/06 //School: University of Phoenix //Class: POS 440: Introduction To C++ //Instructor: John Williams #include #include #include //Header file in order for a user to calculate math formulas using namespace std; int main() { //Variable declarations double LoanAmount; //Loan amount entered by the user in order to calculate a mortgage monthly payment int term; //Years entered by user to use for calcuating the monthly payment double InterestRate; //Interest Rate in percentage used for the loan amount double MortgageMonthlyPayment; cout << "Enter a loan amount: "; cin >> LoanAmount; cout << "Loan amount is: \$ " << LoanAmount << endl; cout << "Enter Interest rate: "; cin >> InterestRate; cout << "Interest rate is: " << right << setw(8) <> term; // Total years for paying off mortgage loan cout << "Your term is: " << right << setw(2) << term << " years" << endl << endl; //Mortgage monthly calculation MortgageMonthlyPayment = (LoanAmount* pow((1+ InterestRate/100/12),term*12)* InterestRate/100/12)/((pow((1+InterestRate/100/12),term*12))-1); //Calculates the monthly mortgage payment cout << "Mortgage monthly payment is: \$ " << MortgageMonthlyPayment << endl; //Allow user to loop back and enter new data or to just quit the program while (letter != 'Q' && letter != 'Z') return 0; //End of program } Nov 1 '06 #1
5 Replies

 P: 7 I am working on an assignment where I have to allow end-users to enter in data and to have C++ make a calculation. From there I have to set up a loop to allow users to go back and enter new data or just quit. I have the part down where the users are entering in information to get a calculation, but I am not understanding the part about looping to go back, any direction will be greatly appreciated. Here is my code for now: //************************************************** ***** //Program: Calculations Payments //Purpose: To calculate a mortgage monthly payment by the user entering input // and to allow the user to enter new data if they wish or to just quit the program. //Programmer: Cameron Faldeti //Date: 10/30/06 //School: University of Phoenix //Class: POS 440: Introduction To C++ //Instructor: John Williams #include #include #include //Header file in order for a user to calculate math formulas using namespace std; int main() { //Variable declarations double LoanAmount; //Loan amount entered by the user in order to calculate a mortgage monthly payment int term; //Years entered by user to use for calcuating the monthly payment double InterestRate; //Interest Rate in percentage used for the loan amount double MortgageMonthlyPayment; cout << "Enter a loan amount: "; cin >> LoanAmount; cout << "Loan amount is: \$ " << LoanAmount << endl; cout << "Enter Interest rate: "; cin >> InterestRate; cout << "Interest rate is: " << right << setw(8) <> term; // Total years for paying off mortgage loan cout << "Your term is: " << right << setw(2) << term << " years" << endl << endl; //Mortgage monthly calculation MortgageMonthlyPayment = (LoanAmount* pow((1+ InterestRate/100/12),term*12)* InterestRate/100/12)/((pow((1+InterestRate/100/12),term*12))-1); //Calculates the monthly mortgage payment cout << "Mortgage monthly payment is: \$ " << MortgageMonthlyPayment << endl; //Allow user to loop back and enter new data or to just quit the program while (letter != 'Q' && letter != 'Z') return 0; //End of program } What you need to do is put your code in a DO WHILE LOOP. You are almost there. A Do While Loop will repeat your option menu until the user decides to quit. something like this; Do { // cout << "Enter a loan amount: "; cin >> LoanAmount;//etc............... } while (letter != 'Q' && letter != 'Z'); Nov 1 '06 #2

 P: 10 Thanks, I will give that a try and let you know how it works out. I appreciate it so much. What you need to do is put your code in a DO WHILE LOOP. You are almost there. A Do While Loop will repeat your option menu until the user decides to quit. something like this; Do { // cout << "Enter a loan amount: "; cin >> LoanAmount;//etc............... } while (letter != 'Q' && letter != 'Z'); Nov 1 '06 #3

 P: 10 I went ahead and tried putting in a do while loop, however when I debug my program, I am getting this message: Microsoft Visual C++ Debug Library Debug Error! Program:.. Module:..n_Files\University of Phoenix\POS 440\week 3\debug\week3.exe File: Run-Time Check Failure #3-The Variable 'number' is being used without being defined. (press Retry to debug the application) Than underneath I have the three buttons: abort, ignore, cancel. Here is my latest code is anybody has any suggestions: //************************************************** ***** //Program: Calculations Payments //Purpose: To calculate a mortgage monthly payment by the user entering input // and to allow the user to enter new data if they wish or to just quit the program. //Programmer: Cameron Faldeti //Date: 10/30/06 //School: University of Phoenix //Class: POS 440: Introduction To C++ //Instructor: John Williams #include #include #include //Header file in order for a user to calculate math formulas using namespace std; int main() { //Variable declarations double LoanAmount; //Loan amount entered by the user in order to calculate a mortgage monthly payment int term; //Years entered by user to use for calcuating the monthly payment double InterestRate; //Interest Rate in percentage used for the loan amount double MortgageMonthlyPayment; int number; cout << "Enter a loan amount: "; cin >> LoanAmount; cout << "Loan amount is: \$ " << LoanAmount << endl; cout << "Enter Interest rate: "; cin >> InterestRate; cout << "Interest rate is: " << right << setw(8) <> term; // Total years for paying off mortgage loan cout << "Your term is: " << right << setw(2) << term << " years" << endl << endl; //Mortgage monthly calculation MortgageMonthlyPayment = (LoanAmount* pow((1+ InterestRate/100/12),term*12)* InterestRate/100/12)/((pow((1+InterestRate/100/12),term*12))-1); //Calculates the monthly mortgage payment cout << "Mortgage monthly payment is: \$ " << MortgageMonthlyPayment << endl; //Allow user to loop back and enter new data or to just quit the program do { cout << "Enter a loan amount: "; cin >> LoanAmount; cout << "Loan amount is: \$ " << LoanAmount << endl; cout << "Enter Interest rate: "; cin >> InterestRate; cout << "Interest rate is: " << right << setw(8) <> term; cout << "Your term is: " << right << setw(2) << term << " years" << endl << endl; }while(number != '1' && number != '2'); cout << "To quit the program enter 1 or 2." << endl; cout << "Enter a number: "; cin >> number; cout << endl; //ends while loop return 0; //End of program } Nov 2 '06 #4

 P: 10 I figured out why I could not get rid of the run-time error, it's because I needed a variable in my int number statement. I believe my program is working the way it should now. The purpose as I mentioned was to create a program that would calculate a monthly mortgage payment with a loan amount of 200000 with an interest rate of 5.75% with a 30 year time period. From there I had to set a loop to allow the user to go back and enter new data or quit. Here is my latest code: //************************************************** ***** //Program: Calculations Payments //Purpose: To calculate a mortgage monthly payment by the user entering input // and to allow the user to enter new data if they wish or to just quit the program. //Programmer: Cameron Faldeti //Date: 10/30/06 //School: University of Phoenix //Class: POS 440: Introduction To C++ //Instructor: John Williams #include #include #include //Header file in order for a user to calculate math formulas using namespace std; int main() { //Variable declarations double LoanAmount; //Loan amount entered by the user in order to calculate a mortgage monthly payment int term; //Years entered by user to use for calcuating the monthly payment double InterestRate; //Interest Rate in percentage used for the loan amount double MortgageMonthlyPayment; int number = 0; cout << "Enter a loan amount: "; cin >> LoanAmount; cout << "Loan amount is: \$ " << LoanAmount << endl; cout << "Enter Interest rate: "; cin >> InterestRate; cout << "Interest rate is: " << right << setw(8) <> term; // Total years for paying off mortgage loan cout << "Your term is: " << right << setw(2) << term << " years" << endl << endl; //Mortgage monthly calculation MortgageMonthlyPayment = (LoanAmount* pow((1+ InterestRate/100/12),term*12)* InterestRate/100/12)/((pow((1+InterestRate/100/12),term*12))-1); //Calculates the monthly mortgage payment cout << "Mortgage monthly payment is: \$ " << MortgageMonthlyPayment << endl; //Allow user to loop back and enter new data or to just quit the program do { cout << "Enter a loan amount: "; cin >> LoanAmount; cout << "Loan amount is: \$ " << LoanAmount << endl; cout << "Enter Interest rate: "; cin >> InterestRate; cout << "Interest rate is: " << right << setw(8) <> term; cout << "Your term is: " << right << setw(2) << term << " years" << endl << endl; MortgageMonthlyPayment = (LoanAmount* pow((1+ InterestRate/100/12),term*12)* InterestRate/100/12)/((pow((1+InterestRate/100/12),term*12))-1); cout << "Mortgage monthly payment is: \$ " << MortgageMonthlyPayment << endl; break; } while(number != '0' && number != '1'); cout << "To quit the program enter 1 or 2." << endl; cout << "Enter a number: "; cin >> number; cout << endl; //ends while loop return 0; //End of program } If anybody has any suggestions or comments, please feel free to let me know. Nov 2 '06 #5

 P: 10 It took me a while to fully understand what you meant, here is my latest code: //************************************************** ***** //Program: Calculations Payments //Purpose: To calculate a mortgage monthly payment by the user entering input // and to allow the user to enter new data if they wish or to just quit the program. //Programmer: Cameron Faldeti //Date: 10/30/06 //School: University of Phoenix //Class: POS 440: Introduction To C++ //Instructor: John Williams #include #include #include //Header file in order for a user to calculate math formulas using namespace std; int main() { //Variable declarations double LoanAmount; //Loan amount entered by the user in order to calculate a mortgage monthly payment int term; //Years entered by user to use for calcuating the monthly payment double InterestRate; //Interest Rate in percentage used for the loan amount double MortgageMonthlyPayment; char quit;//Allows users to quit the program if they wish quit = 'N'; //If users enter N, they will have the option to enter new data quit = 'n'; //If users enter n, they will have the option to enter new data //Allow user to loop back and enter new data or to just quit the program do { cout << "Enter a loan amount: "; cin >> LoanAmount; cout << "Loan amount is: \$ " << LoanAmount << endl; cout << "Enter Interest rate: "; cin >> InterestRate; cout << "Interest rate is: " << right << setw(8) <> term; //Total years for paying off a mortgage loan cout << "Your term is: " << right << setw(2) << term << " years" << endl << endl; //Mortgage monthly calculation MortgageMonthlyPayment = (LoanAmount* pow((1+ InterestRate/100/12),term*12)* InterestRate/100/12)/((pow((1+InterestRate/100/12),term*12))-1); cout << "Mortgage monthly payment is: \$ " << MortgageMonthlyPayment << endl; cout << "Press N if you want to enter new data, or press 1 or press 2 to quit the program." << "Choice is: " << endl; //Gives users a chance to continue or quit cin >> quit; }while(quit!= '1'&& quit != '2'); system("PAUSE"); return 0; //End of program } Nov 3 '06 #6 