[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# loop to alway to go back into the program

Posted on 2006-04-06
Medium Priority
315 Views
I am trying to add a loop to allow the user to re-enter the information.

#include <math.h>
#include <string>
#include <iostream>
#include <stdlib.h>

//Start of program
using namespace std;
int main(void)
{

// Set up required variables
double  loanAmount; // loan amount
int Years;          // Years of loan
double  IntRate;    // Interest rate
double MonthRate;   // Months of loans
double MonthTerm;   // Term of loan

//Infomation need for Monthly Mortgage Payment

cout << "Enter the loan amount in dollars: ";
cin >> loanAmount;

cout << "Enter the loan term in years: ";
cin >> Years;

cout << "Enter the interest rate: ";
cin >> IntRate;

// Calculate Monthly Mortgage Payment

MonthTerm = Years * 12;
MonthRate = IntRate / (12 * 100);

double PowCalc = pow(1+MonthRate,MonthTerm);
double paymentAmount = ((MonthRate*PowCalc) / (PowCalc-1)) * loanAmount;
//Information give on  Monthly Mortgage Payment
system("cls");
cout << endl;   //space
cout << "Loan amount of \$" << loanAmount << endl;
cout << endl;   //space
cout << "For a term of " << Years << " years" << " (" << Years*12 << " months)" << endl;
cout << endl;   //space
cout << "With an interest rate of " << IntRate << "%" << endl;
cout << endl;   //space
cout << "Monthly mortgage payment of \$" <<paymentAmount << " per month." << endl;
cout<<"Enter C to continue, Q to quit>";
}

while ((quit!='q')&&(quit!='Q')&&(quit!='c')&&(quit!='C') ){       //loop for check of valid user input
cout<<"Enter C to continue, Q to quit>";

cin>>quit;       //retrieve user input
cout<<'\n';
}

}
return 0;
0
Question by:Shannon3077
• 2

LVL 3

Expert Comment

ID: 16391679
You prog. is not well structured.

Intialize
quit="X";
Place while loop as
while ((quit!='q')&&(quit!='Q')&&(quit!='c')&&(quit!='C') ){
cout << "Enter the loan amount in dollars: ";
cin >> loanAmount;

cout << "Enter the loan term in years: ";
cin >> Years;

cout << "Enter the interest rate: ";
cin >> IntRate;
...

...
cout<<"Enter C to continue, Q to quit>";
}
0

LVL 52

Accepted Solution

Carl Tawn earned 200 total points
ID: 16392544
That doesn't make any sense. The loop you have will continue as long as the user doesn't enter q/Q or c/C.

Try it like:

#include <math.h>
#include <string>
#include <iostream>
#include <stdlib.h>

//Start of program
using namespace std;
int main(void)
{

// Set up required variables
double  loanAmount; // loan amount
int Years;          // Years of loan
double  IntRate;    // Interest rate
double MonthRate;   // Months of loans
double MonthTerm;   // Term of loan
char quit;

while (true)
{
//Infomation need for Monthly Mortgage Payment

cout << "Enter the loan amount in dollars: ";
cin >> loanAmount;

cout << "Enter the loan term in years: ";
cin >> Years;

cout << "Enter the interest rate: ";
cin >> IntRate;

// Calculate Monthly Mortgage Payment

MonthTerm = Years * 12;
MonthRate = IntRate / (12 * 100);

double PowCalc = pow(1+MonthRate,MonthTerm);
double paymentAmount = ((MonthRate*PowCalc) / (PowCalc-1)) * loanAmount;
//Information give on  Monthly Mortgage Payment
system("cls");
cout << endl;   //space
cout << "Loan amount of \$" << loanAmount << endl;
cout << endl;   //space
cout << "For a term of " << Years << " years" << " (" << Years*12 << " months)" << endl;
cout << endl;   //space
cout << "With an interest rate of " << IntRate << "%" << endl;
cout << endl;   //space
cout << "Monthly mortgage payment of \$" <<paymentAmount << " per month." << endl;

do
{
cout << "Enter C to continue, Q to quit>";
cin >> quit;

} while ((quit!='q') && (quit!='Q') && (quit!='c') && (quit!='C'));

if (quit == 'q' || quit == 'Q')
break;
}

cout << "Goodbye!";

return 0;
}
0

LVL 3

Expert Comment

ID: 16392783
Carl tawn ,
You meant ,
cin>>quit;       //retrieve user input
cout<<'\n';

It has to be the last lines of while loop, which is quite obivious .
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month18 days, left to enroll