Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm making a mortgage calculator for my class, the program runs but the answer isn't correct, I think I have the right formula but maybe I'm declaring my variables wrong.
This is what I have so far
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
//Variables
double rate;
rate = rate / 1200.0;
double term;
term = term * 12;
double principal;
double monthlyPayment;
monthlyPayment = (principal * rate) / (1.0 - pow(rate + 1, -term));
//Gathering Values
cout << "Please enter your interest rate." << endl;
cin >> rate;
cout << "Please enter your term." << endl;
cin >> term;
cout << "please enter your principal" << endl;
cin >> principal;
//Formula
monthlyPayment = principal * rate / (1.0 - pow(rate + 1, -term));
//Result
cout << "Your monthly payment is, " << monthlyPayment << endl;
return 0;
}
All of the math you do before the cin statements is not being factored in to your calculation. Also, your second formula calculating monthlyPayment is missing parentheses in the numerator.
Related
This question already has answers here:
Why not use Double or Float to represent currency?
(16 answers)
Closed 3 years ago.
The code seems to work fine when it performs the first step of multiplying the number of quarters entered by 0.25, but then it just doesn't work with the next two steps.
#include <iostream>
using namespace std;
int main()
{
int quarter, dime, nickle;
int result;
quarter = 25;
dime = 10;
nickle = 5;
int numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
result = (numQuarters * quarter) + (numNickles * nickle) + (numDimes * dime);
cout << "The total amount of pennies is: " << result;
return 0;
}
I expect the output of 4 quarters, 10 dimes & 20 nickels to be 300 pennies
The output is 102
Edit: Code Fixed and working now!
Hmm... your code seems fine, but I think something was wrong with the Order of Operations in your Math. I just changed the value of nickel to 0.05 and the value of dime to 0.10 (I think that was a mistake in your code). I aslo moved the *100 down to the cout statement, and that helped clear things up...
#include <iostream>
using namespace std;
int main()
{
float quarter, dime, nickle, penny;
float result;
quarter = 0.25;
dime = 0.10;
nickle = 0.05;
float numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
result = (numQuarters * quarter) + (numNickles * nickle)+ (numDimes * dime);
cout << "The total amount of pennies is: " << result * 100;
return 0;
}
Oh, and just like what #Pete Becker said, do your calculations in pennies (whole numbers). Not only does it fix some minor errors with floats as money, it also makes your code easier to read and manipulate.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Basically I want to square any number I input. Why does this not work?
I compiles but it does not square my input.
#include <iostream>
using namespace std;
float SquareNumber;
float Squared = SquareNumber * SquareNumber;
int main()
{
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
cout << SquareNumber << " squared is " << Squared;
system("pause");
return 0;
};
I compiles but it does not square my input.
You need to compute the square after you input the number. At the time Squared is computed, the value of SquaredNumber is 0.0. Hence, the value of Squared is also 0.0.
The line
float Squared = SquareNumber * SquareNumber;
sets the value of Squared using the value of SquaredNumber at that time. It does not update the value of Square when the value of SquaredNumber is changed. To get that effect, you need to use a function.
#include <iostream>
using namespace std;
float SquareNumber;
float Squared(float in)
{
return in * in;
}
int main()
{
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
cout << SquareNumber << " squared is " << Squared(SquareNumber);
return 0;
};
Your problem is that you are not correctly calculating the square. You do it as "compile time", rather than at "run time".
Just change your code to this, but be sure that you understand why - and ask here if you do not (hint: how can you calculate the square of a number, before you know what the number is?).
#include <iostream>
using namespace std;
int main()
{
float SquareNumber;
float Squared;
cout << "Please enter a number to be squared: ";
cin >> SquareNumber;
Squared = SquareNumber * SquareNumber; // calculate at *run time*
cout << SquareNumber << " squared is " << Squared;
system("pause");
return 0;
};
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
my while loop wont run on the conditions I set it.
The purpose of the program is to determine the amount of years it will take a deposited amount to mature using the deposit amount, interest rate and target amount.
The program just stops after the target amount has been entered, unless I change the while statement from <= to >= in which case it runs the loop but returns the number of years set at round 100's or 1000 etc...
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
using namespace std;
int main()
{
//declare the variables
double rate,
balance = 0;
int deposit,
target,
years = 0;
cout << "****Lets make you some money!****" << endl << endl;
//input from the user
cout << "What is your deposit amount?: " << endl;
cin >> deposit;
cout << "What is your interest rate?: " << endl;
cin >> rate;
cout << "What is you target savings amount?: " << endl;
cin >> target;
rate = rate / 100;
while (balance <= target); //when i change this to balance >= target the 'while' runs but just returns years divisible by 100
{
// calculation
balance += deposit * pow((1 + rate), years);
//balance = balance*(1 + rate) + deposit; // alternate calculation
//years++;
//users savings target
cout << "You will reach your target savings amount in: " << balance << " years." << endl << endl << " That's not that long now is it?" << endl;
}
return 0;
}
Thanks in advance!
The problem is an unfortunate suffix:
while (balance <= target);
// ^
That is equivalently:
while (balance <= target) {
;
}
{
// calculation, which always runs exactly once
// regardless of what balance/target are
}
Just drop the semicolon.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So, here is the updated thread: Maybe there is white space in there that I'm not seeing? It is the exact same error as it was before. You anyone can think of anything to try, ill do it.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// 1. Initialize Objects
double retail = 0.0;
double markup = 0.0;
double wholesale = 0.00;
// 2. Input
cout << "Hello Emily CO 127: ";
cout << "Enter Retail Price: ";
cin >> retail;
cout << "Enter Mark Up Percent: ";
cin >> markup;
// 3. Process
wholesale = retail * (1 + markup);
cout << " Wholesale " << wholesale << endl;
return 0;
system("pause");
}
using namespace std; is a declaration that introduces the identifiers from the namespace called std in the global scope. It is not the beginning of function or starting point for a block. What you're missing is your main function:
int main() // start of the program
{
// ...
}
This is what your program should look like then:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// 1. Initialize Objects
double retail = 0.0;
double markup = 0.0;
double wholesale = 0.00;
// 2. Input
cout << "Hello Emily CO 127: ";
cout << "Enter Retail Price: ";
cin >> retail;
cout << "Enter Mark Up Percent: ";
cin >> markup;
// 3. Process
wholesale = retail * (1 + markup);
cout << " Wholesale " << wholesale << endl;
return 0;
system("pause");
}
You should read up more on the basics of C++, they will explain everything you need to know.
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char *argv[])
{
double amount, rate, time, interest, month;
interest = rate/(12.0*100);
month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);
cout << "What is the amount of the loan? $ ";
cin >> amount;
cout << "What is the annual percentage rate? ";
cin >> rate;
cout << "How many years will you take to pay back the loan? ";
cin >> time;
cout <<"\n-------------------------------------------------------\n";
cout << "Amount Annual % Interest Years Monthly Payment\n\n\n";
cout <<amount <<" " <<rate <<" " <<time << " " <<month;
cout <<"\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
I am getting an error here and am not sure exactly how to use the pow function properly. The formula according to the problem is:
Monthly payment = (loan amount)(monthly interest rate) / 1.0 - (1.0 + Monthly interest rate)^-(number of months)
Here is the line that I am having trouble with
month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);
Thanks for the help
std::pow accepts two arguments like so
pow (7.0,3)
So your code should be more like this
month = (amount * rate)/1.0 - std::pow( (1.0 + interest), 12);
Your code has other flaws as well, like you do calculation before you set the values.