I am obviously new :( It is my formula, but this has been the only way I can even get it to run at all. It just displays an insanely big number and I don't understand how to write it. Please have mercy on my newbie soul...
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(int argc, char** argv) {
float F, P, i;
int t;
cout << "Enter how much money is currently in the account: ";
cin >> P;
while (P < 1) // Amount can not be less that $1
{
cout << "Value must be at least $1, enter another amount: ";
cin >> P;
}
cout << "Enter the monthly interest rate: ";
cin >> i;
cout << "Enter how many months this money will be in the account: ";
cin >> t;
F = P * pow( 1 + i,(t * 12));
cout << fixed << showpoint;
cout << "Original:" << setfill(' ') << setw(20) << "$" << P << endl;
cout << "Monthly Interest:" << setfill(' ') << setw(12) << "$" << i << endl;
cout << "Future amount:" << setfill(' ') << setw(15) << "$" << F;
return 0;
}
As already mentioned in the comments, the issue is located in your interest formula. Please change it to the following:
F = P * pow( 1 + i/(P * 12),(t * 12));
As you insert the monthly interest rate i in $, hence as a absolute value, you need to calculate the relative one i / P. Also the compounding frequency is missing, which shall be every month in your question. Therefore the 12 has to be added in the denominator.
Please have a look to Wikipedia for further information, especially in section Calculation.
Hope it helps.
Related
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 5 years ago.
Improve this question
There is a program I'm building in which I need to calculate torque and diameter. I have both equations in syntax but my output keeps being zero. What am I missing / doing wrong? (Disclaimer: I'm very new to this so please, I welcome constructive criticism! Anything to help me get better. Thanks).
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
//Output:
/*Enter values for horsepower (p), rpm (n) and shear strength (s): 20 1500 5000
HP 20
rpm 0
psi 0
torque inf
diameter inf */
The intent of the lines
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
is not correctly represented in code.
The second line does not read anything into n or s. It's an expression that uses the comma operator. In this particular case, it evaluates n and s and discards the values.
It's as if you have:
cin >> p;
n;
s;
Change that line to:
cin >> p >> n >> s;
You can also be verbose and use:
cin >> p;
cin >> n;
cin >> s;
Take your input like this (cin >> p >> n >> s) way and also print your result after result calculation you are are printing before.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
// cin >> p, n, s;
cin >> p >> n >> s;
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
I have a problem with my code, every time I loop it with the answer 'y'(Yes) it loops to infinity?
I'm trying to make a loan calculator and every time the user is done calculating with a transaction and wants to reset, and do another calculation if he enters in a value 'y', and if he enters 'n' the program will end.
Here's my code so far:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
char ans = 'y';
do {
string name;
int Months;
int n;
double LoanAmount, Rate, MonthlyInterest, TotalLoanAmount, MonthlyAmortization, OutstandingBalance;
cout << fixed << showpoint;
cout << "Enter Name of Borrower: ";
getline(cin, name);
cout << "Enter Loan Amount: ";
cin >> LoanAmount;
cout << "Enter Number of Months to Pay: ";
cin >> Months;
cout << "Enter Interest Rate in Percent(%): ";
cin >> Rate;
cout << setprecision(2);
MonthlyInterest = LoanAmount * Rate;
TotalLoanAmount = LoanAmount + (MonthlyInterest * Months);
cout << "Monthly Interest: " << MonthlyInterest << endl
<< "Total Loan Amount with interest: " << TotalLoanAmount << endl;
cout << setw(100)
<< "\n\tSUMMARY OF OUTSTANDING INSTALLMENT" << endl
<< "\tName of Borrower: " << name
<< "\n\nMonth\t\tMonthly Amortization\t\tOutstanding Balance"
<< "\n";
for(n = 1; n <= Months; n++) {
MonthlyAmortization = TotalLoanAmount / Months;
OutstandingBalance = TotalLoanAmount - MonthlyAmortization;
cout << n << "\t\t" << MonthlyAmortization << "\t\t\t" << n - 1 << OutstandingBalance << endl;
}
cout << "\nEnd of Transaction";
cout << "Do you want to compute another transaction?[y/n]?" << endl;
cin >> ans;
}
while(ans == 'y');
}
After your cin>>ans, add these two lines :
cin.clear();
cin.sync();
That usually fixes a lot of the infinite looping problems I get with cin.
Also, I would recommend against initializing ans as 'y' when you declare it. I don't think this is causing you problems but it's an uncessesary thing.
You seem to expect pressing y and enter to register as only 'y'. If you want to get the input of just one character have a look at std::cin.get(char)
I'm attempting to create a program to figure out Delta-V for my Kerbal Space Program game, and C++ (being run in the Eclipse IDE) does not allow for use of log() without assuming I'm trying to call a function. Thank you so much for help! It's really nice of you.`
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << "Hello. Welcome to the Kerbal Space Program Delta V Calculator. \n";
cout << " \n";
cout << "Note that each stage must use the same engine for this calculator.";
cout << "\n";
cout << "\nHow many stages make up your rocket? :";
int stageNumber;
cin >> stageNumber;
//cout << "Your rocket has " << stageNumber << " stages.\n";
cout << "\n\nStart from the bottom stage, please. ";
//ACTUAL DELTA V CALCULATIONS
for(int currentStage = 1; currentStage <= stageNumber; currentStage = currentStage + 1){
cout << "What is the total mass of this stage? :";
int totalMass;
cin >> totalMass;
cout << "What is the fuel mass of this stage? :";
int fuelMass;
cin >> fuelMass;
cout << "\n";
int dryMass;
dryMass = totalMass - fuelMass;
cout << "Your dry mass is" << dryMass << "\n";
cout << "\n";
cout << "Give the specific impulse of this stage's engine. \n";
int iSP;
cin >> iSP;
cout << "Here is the Delta V of your rocket.\n";
int deltaMass;
deltaMass = totalMass/dryMass;
int deltaV;
deltaV = iSP * log(deltaMass);
cout << deltaMass
exit(0);
}
}
`
log() is a function in the C standard library that takes the natural logarithm of a number. The name is effectively reserved — pick something else.
I want make three items to calculate then convert
Example:
item1 + item2 + item3 = `total`
Then, convert total to Saudi riyals
// sss.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
using namespace std;
int main()
{
cout << "conver from US dollar to Saudi ryals" << endl;
double dollars1;
double dollars2;
double dollars3;
double ryals = 3.75;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars1;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars2;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars3;
cout << "US $" << dollars1 + dollars2 + dollars3 << " equals " << ryals * (dollars1 + dollars2 + dollars3) << " Saudi ryals." << endl;
cout << "that's it" << endl;
cin.get();
cin.get();
}
Today's exchange rate for US dollar to Saudi riyal is $1 US to 3.75 riyal.
Your logic seems correct here : ryals * ( dollars1 + dollars2 + dollars2) but you for starters have two mains. This cannot happen in any circumstance. Also I'd re-think your naming convention of dollars1, dollars2, dollars3
Your #include <stdafx> was auto generated by Visual Studio, which is why I'm assuming two mains were created for you. See here for more information.
Try this:
#include "iostream"
using namespace std;
int main()
{
cout << "conver from US dollar to Saudi ryals" << endl;
double dollars1;
double dollars2;
double dollars3;
double ryals = 3.75;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars1;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars2;
cout << "Enter the amount of money you would like to transfer item 1" << endl;
cin >> dollars3;
cout << "US $" << dollars1 + dollars2 + dollars3 << " equals " << ryals * (dollars1 + dollars2 + dollars3) << " Saudi ryals." << endl;
cout << "that's it" << endl;
cin.get();
}
I have quite straight forward question. The following code prints out celsius and fahrenheit.
My question is though about number of times it iterate. For a small number e.g. start 0, stop at 10, with a step of 1.1. After the loop is finished it will print out the correct number of iterations it made.
But for large number 0-11000000, with step 1.1 it will print out wrong number of iteration. Why is that happening? Since 1100000/1.1 should be around 1000001, but I get 990293.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float start, stop, step;
int count = 0;
cout << "start temperature: ";
cin >> start;
cout << "stop temperature: ";
cin >> stop;
cout << "step temperature: ";
cin >> step;
cout << setw(10) << "celsius" << setw(15) << "fahrenheit" << endl;
cout << setw(25) << "celsius" << setw(15) << "fahrenheit" << endl;
while(start <= stop)
{
count++;
float c, f;
c = (5.0/9)*(start-32);
f = 32+(9.0/5)*start;
cout << setw(10) << fixed << setprecision(2) << c << setw(15) << start << setw(15) << fixed << setprecision(2) << f << " count: " << count << endl;
start = start + step;
}
cout << "The program loop made " << count << " iterations." << endl;
return 0;
}
Floating-point rounding error. Essentially, floats are not a 100% accurate representation, there are errors in every calculation you make, and as you repeatedly add to them, you will be adding more and more error. What you should do is compute the number of steps once, store it in an integer, and then loop that many times.
For the record, a cleaned-up version would look like:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double start, stop, step;
cout << "start temperature: ";
cin >> start;
cout << "stop temperature: ";
cin >> stop;
cout << "step temperature: ";
cin >> step;
cout << setw(10) << "celsius" << setw(15) << "fahrenheit" << endl;
unsigned steps = (stop - start) / step;
for(unsigned i = 0; i < steps; ++i)
{
double temp = start + i * step;
double c = (5.0 / 9.0) * (temp - 32.0);
double f = 32.0 + (9.0 / 5.0) * temp;
// This is a real good example of why people hate <iostream> formatting.
// If you want formatting that's quite different from the default, it
// gets too verbose too fast. Using C stdio:
//printf("%10.2f%15.2f\n", c, f);
cout << setw(10) << fixed << setprecision(2) << c
<< setw(15) << fixed << setprecision(2) << f << endl;
}
cout << "The program loop made " << steps << " iterations." << endl;
return 0;
}
The main benefit of this style of loop is that each iteration is (except for the output) order-independent, so it could be unrolled and (theoretically) parallelized.