Hello everyone....

I have gotten this far with my mortgage calculator....the problem is the while loop and the actual calculation isn't working correctly. The while loop really doesnt do what i was hoping for it to do...and being pretty amature at this...i am not sure where to go. i built this program in the first week of class with hard coded varibles in the code, and it worked just fine....now that the user needs to enter the code...it doesnt do so well....and help would be great. Thank you to everyone!!

/*
 Write the program as a procedural C++ program. Calculate 
 and display the mortgage payment amount using the amount 
 of the mortgage, the term of the mortgage, and the interest 
 rate of the mortgage as input by the user. Allow the user to 
 loop back and enter new data or quit. Insert comments in the
 program to document the program.
 */


#include <iostream>
#include <cmath>


using namespace std;

int main () {
	
	double principle = 0;
	double interest = 5.7;
	int term = 30 * 12;
	double total;
	double monthlyInterest = interest * 12;
	bool validNum = false;

	cout << "Please enter principle amount: ";
	
	cin >> principle;
	
	cout << "You entered: " << principle << endl << endl;
	
	
	
	while (validNum == false) {
		
		cout << "Please enter a positive number: ";
		
		cin >> principle;
		
		cout << "You entered: " << principle << endl << endl;
		
		
		
		if (principle <= 0) {
			
			cout << "You did not input a correct number"
			
			<< endl;
			
			cout << "Please re-enter the number!" << endl << endl;
			
		}
		else 
			validNum = true;
		
		
		
	}
	
	cout << "Enter desired Interest Rate: " << endl;
	cin >> interest;
	
	while (validNum == false) {
		
		cout << "Please enter a positive number: ";
		
		cin >> interest;
		
		cout << "You entered: " << interest << endl << endl;
		
		
		
		if (interest <= 0) {
			
			cout << "You did not input a correct number"
			
			<< endl;
			
			cout << "Please re-enter the number!" << endl << endl;
			
		}
		else 
			validNum = true;
		
		
		
	}
	
	
	//cout << "Thank you for entering a valid number!" << endl;
	
	total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term));

	
	cout << "Your priniple is " << principle << endl;
	cout << "Your Interste rate is: " << interest << endl;
	cout << "Term Length: " << term << " Months" << endl;
	cout << "Your total monthly payment is: " << total << endl;
	
	
    return 0;
	
}

> double monthlyInterest = interest * 12;
Not divide by 12 ?

> double interest = 5.7;
I think this is 570%
Seems high to me.

In order to allow the user to enter new data or quit, you will want to place all of your code after your variable declarations in a while loop. That way, the user can go through the program as many times as he or she wants:

#include <iostream>
#include <cmath>  
#include <string>

using namespace std; int main () 
{ 	
      double principle = 0;	 
      double interest = 5.7;	
      int term = 30 * 12;	
      double total;	
      double monthlyInterest = interest * 12;	
      bool validNum = false;
      //create a yesNo variable to allow the user to select
     //if her or she wants to keep using the program
      string yesNo = "y"

      while(yesNo == "y")
      {
          //contents of the program, then add question at the
          //end asking user if they would like to use it again
      }

Hope that helps!

-D

Okay, I understand the the yes no part. But when i run the current loop i have it doesnt seem to work right. What is it i am doing wrong with it? I have tried so many different things, but i just cant seem to get it to work.

Okay, I understand the the yes no part. But when i run the current loop i have it doesnt seem to work right. What is it i am doing wrong with it? I have tried so many different things, but i just cant seem to get it to work.

What exactly is the program doing that you don't want it to?

-D

You haven't said anything about the desired output of your program, the total you were expecting, what testsdid you do , if you ae using compound interest, then the formula looks wrong to me.

Whether the interest rates are given directly or you are supposed to divide by 100 and then store those values, whether the compounding is done monthly or annually is still not clear going by

int term = 30 * 12;	//30 years right?
double monthlyInterest = interest * 12; //what exactly for and 
//it should be annualInterest

Did you read your own thread
http://www.daniweb.com/forums/thread210871.html

Hey everyone. Thank you for the help so far i really am very grateful for all of it. I think i have the main part of the program working. The part I am having a problem with is the calculation for the total loan. I need to calculate the total per month of the loan. When i do the calculation at the end it gives me a number like 1.368e+07 and i am not sure how to correct this. Can someone help me on this thanks.

#include <math.h>
#include <iostream>
using namespace std;


int main () {
	double principle = 0;
	double interest = 5.7;
	int term = 30 * 12;
	double total;
	//double interest = i * 100;
	double monthlyInterest = interest * 12;
	
	int choice;
	
	do {
		cout << "Please enter total loan amount" << endl;
		cin >> principle;
		cout << "Your entered: " << principle << endl;
		cout << "Please enter interest rate " << endl;
		cin >> interest;
		cout << "Your entered: " << interest << endl;
		cout << "Please enter the term in years: " << endl;
		cin >> term;
		cout << "Your entered: " << term << endl;

		
		
		
		total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term)); //total for mortgage
		
		

		cout << "Your total monthly payment is: " << total << endl;
		
		cout << "Would you liek to play again?" << endl;
		cout << "1 for yes" << endl; //gives user choice
		cout << "2 for no" << endl; //gives user choice
		cin >> choice;
		
		

		
	}while (choice==1);
	
    return 0;
}

If you want fixed point then user

cout.setf(std::ios_base::fixed,std::ios_base::floatfield);

This will output number such as 1.2000000000 instead of
1.2e10

Okay i can put that in there, but the answer is still not correct. In the equation what am i doing wrong?

Give me an input values and what the output value supposed to be.

Correct me if I'm wrong, but it seems like you have your monthly interest set up as 12 times your yearly interest, or 12 years worth of interest per month:

double monthlyInterest = interest * 12;

Shouldn't it be your yearly interest divided by 12 (1/12 of the yearly interest per month?):

double monthlyInterest = interest / 12;

Perhaps I'm wrong about that, but it seems like your problem may be stemming from there...

-D

before your second while loop shouldn't you reassign your bool variable to false.

Okay for my first assignment i did this the following code. And it gave the correct answer. All i have done is accepted user input instead of hard coding it in.

double principle = 200000; //total principle double interest = 0.0575; //intrest rate int term = 30 * 12; //total term double total; double monthlyInterest = interest / 12; //monthly interest calculation

total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term)); //total for mortgage //output for calculation cout << "Loan amount:\t$" << principle <<endl; cout << "Intrest Rate:\t" << interest <<endl; cout << "Term Length:\t" << term << " Months" <<endl; cout << "Total Monthly Payment:\t"<< total<< endl; return 0; [\code][code=c++]
double principle = 200000; //total principle
double interest = 0.0575; //intrest rate
int term = 30 * 12; //total term
double total;

double monthlyInterest = interest / 12; //monthly interest calculation

total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term)); //total for mortgage

//output for calculation
cout << "Loan amount:\t$" << principle <<endl;
cout << "Intrest Rate:\t" << interest <<endl;
cout << "Term Length:\t" << term << " Months" <<endl;
cout << "Total Monthly Payment:\t"<< total<< endl;

return 0;
[\code]

Right, note how on the code you just posted for the hard coded variables that you have monthlyInterest defined as the following:

double monthlyInterest = interest / 12

Whereas in the code you posted for the user-defined variables, you have monthlyInterest defined as follows:

double monthlyInterest = interest * 12

Those two definitions will yield drastically different results...

-D

Thank you so much for the help. I finally got it to run after changing somethings around. Now i just read that i need to be able to break my code....not really sure how to go about this. If the user inputs a letter there needs to be an error. Same with a special character. Can you guys provide any help with that. Thank you!

Here is the code finally working

/*
 Write the program as a procedural C++ program. 
 Calculate and display the mortgage payment amount 
 using the amount of the mortgage, the term of the 
 mortgage, and the interest rate of the mortgage as 
 input by the user. Allow the user to loop back and 
 enter new data or quit. Insert comments in the program 
 to document the program.
 */ 

#include <math.h>
#include <iostream>
using namespace std;


int main () {
	
	//Declaire variables
	double principle = 0.0; //principle
	double interest = 0.0; //interest rate
	int years = 0;
	//int term = years * 12; //term Years * 12 months to get total months
	double total; //Total monthly payment
	int choice; // Choice for continue or program end
	
	do {	
		
		//Customer input
		cout << "Please enter the term in years: ";
		cin >> years;
		cout << "Your entered: " << years << " Years" << endl;
		cout << "Please enter total loan amount: $";
		cin >> principle;
		cout << "Your entered: $" << principle << endl;
		cout << "Please enter desired interest rate ";
		cin >> interest;
		cout << "Your entered: " << interest << "%" << endl;
		
		double monthlyInterest = (interest / 100) / 12;  // Total monthly interest rate	
		int term = years * 12; //term Years * 12 months to get total months
		
		
		//total Calculation
		total = (principle * monthlyInterest)/(1-pow(1 + monthlyInterest, -term)); //total for mortgage
		
		//Printed Statement
		cout << "Your total monthly payment is: $" << total << endl;		
		cout << "Would you like to play again?" << endl;
		cout << "1 for yes | 2 for no:" << endl; //gives user choice
		//cout << "2 for no" << endl; //gives user choice
		cout << "Make selection and press enter ";
		cin >> choice;
		
		//Choice to quit or continue	
	}while (choice==1);
	
    return 0;
}
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.