PhantomDiclonius
PhantomDiclonius

Reputation: 149

Having issues with mathematical calculations and setprecision() function

I seem to be having a problem with a C++ coding question. It involves mathematical arithmetic and I seem to be getting all of my outputs correct except the final one. In addition to this, the decimal point format of my answers seem to be incorrect. The answers should contain two decimal places but only two out of my four decimal point answers seem to have two decimal places. When I try to use the precision() function, the answers go into scientific notation which I do not want.

Here is the question and answer: enter image description here

Here is my code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    float principal;
    float interest_rate;
    float times_compounded;

    cout << "Hello, please enter a value for your principal: ";
    cin >> principal;
    cout << principal << endl;
    cout << "Please enter a value for your interest rate: ";
    cin >> interest_rate;
    cout << interest_rate << "%" << endl;
    cout << "Please enter the number of times the interest is compounded during the year: ";
    cin >> times_compounded;
    cout << times_compounded << endl << endl;

    float interest = interest_rate * 10.197647;
    float amount = principal * pow((1 + (interest_rate/times_compounded)), times_compounded);

    cout << "Interest Rate: " << setw(19) << interest_rate << "%" << endl;
    cout << "Times Compounded: " << setw(17) << times_compounded << endl;
    cout << "Principal: " << setw(17) << "$ " << setw(7) << principal << endl;
    cout << "Interest: " << setw(20) << "$ " << interest << endl;
    cout << "Amount in Savings: " << setw(9) << "$ " << amount;

    return 0;
}

Here are my three inputs: 1000, 4.25, 12

Any feedback would be appreciated, thank you for your time.

Upvotes: 0

Views: 434

Answers (1)

Toribio
Toribio

Reputation: 4078

First, the last value is wrong because you're using the interest rate as a normal number in the formula although it's actually a percentage. So you'd need to divide it by 100:

float amount = principal * pow((1 + ((interest_rate / 100) /times_compounded)), times_compounded);

Now for the precision, you can use std::fixed in conjunction with std::setprecision to set the default floating point printing precision when using std::cout. We can use a macro to make it more readable, like:

#define FIXED_FLOAT(x, p) std::fixed<<std::setprecision(p)<<(x)

So, the full output section would look like:

cout << "Interest Rate: " << setw(19) << FIXED_FLOAT(interest_rate, 2) << "%" << endl;
cout << "Times Compounded: " << setw(17) << FIXED_FLOAT(times_compounded, 0) << endl;
cout << "Principal: " << setw(17) << "$ " << setw(7) << FIXED_FLOAT(principal, 2) << endl;
cout << "Interest: " << setw(20) << "$ " << FIXED_FLOAT(interest, 2) << endl;
cout << "Amount in Savings: " << setw(9) << "$ " << FIXED_FLOAT(amount, 2);

Also, that interest = interest_rate * 10.197647 seems fishy. Interest should just be the amount minus the principal.

Upvotes: 1

Related Questions