473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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

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 <iostream>
#include <iomanip>
#include <cmath> //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) <<InterestRate << "%" << endl << endl;
cout << "Enter term: ";
cin >> 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 2245
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 <iostream>
#include <iomanip>
#include <cmath> //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) <<InterestRate << "%" << endl << endl;
cout << "Enter term: ";
cin >> 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
cameron
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
cameron
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 <iostream>
#include <iomanip>
#include <cmath> //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) <<InterestRate << "%" << endl << endl;
cout << "Enter term: ";
cin >> 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) <<InterestRate << "%" << endl << endl;
cout << "Enter term: ";
cin >> 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
cameron
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 <iostream>
#include <iomanip>
#include <cmath> //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) <<InterestRate << "%" << endl << endl;
cout << "Enter term: ";
cin >> 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) <<InterestRate << "%" << endl << endl;
cout << "Enter term: ";
cin >> 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
cameron
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 <iostream>
#include <iomanip>
#include <cmath> //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) <<InterestRate << "%" << endl << endl;
cout << "Enter term: ";
cin >> 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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fran Tirimo | last post by:
I am developing a small website using ASP scripts to format data retrieved from an Access database. It will run on a Windows 2003 server supporting FrontPage extensions 2002 hosted by the company...
12
by: Berj | last post by:
i have a novice question. is Access multi-user? can multiple users access the same database?
1
by: sige0008 | last post by:
Hi folks: I have figured out how to sacle glyph by one scalar array, and color by another scalar array. but when I want to change color by third scalar array, I met some problems. At the...
1
by: cameron | last post by:
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...
2
by: shblack | last post by:
I need help with a program I am writing for school. The program has to do the following: /* 1. Character string must be a minimium of 15 charactors. 2. If not 15 characters long it must give...
16
by: simpleeshelbee | last post by:
Hey all, This is my first post. I am really hoping that someone out there has a solution to my C++ coding problem... This is my code and what I have to do... I think it is rows of 1 - 10 and...
6
by: jackj | last post by:
Hi, I am first time C++ student and doing the usual tasks. This one is to create a triangle based on user input of how large (how many rows) and what symbol to use. I have managed to create a...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.