>I am very new using C++ and I am doing my first programs but in this case I am running the following program with problems:
>
>Prompt for user to enter the mortgage amount
>Prompt the user to enter the interest rate
>
>but the problem it is that the result I get is this
>
> -1 #.IND
>
> I can not find the reason for it.... Please help! :-)
>
>#include <iomanip>
>#include <iostream>
>#include <cmath>
>using namespace std;
>
> int main()
>
> {
>
> double r;
> r = 160;
> int y = 30;
> int n = y * 12.0;
> double P ;
>
> cout <<"Enter the Amount of your Loan : $ ";
> cin >>P;
> cin.ignore(1000,10);
>
>
> cout << "Enter your Interest Rate (e.g. 6.0, 7.5.etc.): ";
> cin>> r;
> cin.ignore(1000,10);
>
> cout << (P*pow((1+r) ,n)* r/(pow((1+ r) , n) -1));
>
>
> cout <<fixed;
> cout <<setprecision (2);
>
> return 0;
>
> }
>
I do not know what exactly your result should be. (A hint would be nice)
But one thing is obvious. If I enter for instance 6.0 as the interest rate, your programm runs with a monthly rate of 600 %. This can not result in a correct result.
You have to divide r by 1200 in order to get the monthly rate. Only then you will get 1.005 instead of 7 for the expression (1+r).
This is th enew result I am getting
m = 2.62964e+291.
This is the result I would like to see when I run the program
Your Monthly payment is $ 3078.59
your interest is 6.25%
your loan Term is 30 years
___________________________________________________________
This is my program
#include <iomanip>
#include <iostream>
using namespace std;
#include <cmath>
int main ()
{
// for use in unformatting
#ifdef _WIN32
const int UNFORMAT = cout.flags();
#endif
#ifdef __GNUC__
const std::_Ios_Fmtflags UNFORMAT = cout.flags();
#endif
// unformat, so show a number "as is"
cout << setprecision(-1); // unsets precision
cout.flags(UNFORMAT); // turns off "fixed"
setprecision(2);
//read an int value from the keyboard
double apr = 0;
double interest = (0 * 100.0) /12; // r = calculate nterest 6%*100
double years = 30; // y = Loan Term
int months = 30*12; // n = number of months = 360
double loan = 0; //P = Loan Amount
double m = 0; //Monthly Payment