Hey all. Got a program to run fine, but I'm having to print it in dollars and cents. However, obviously anything other than an int prints multiple places beyond the zero.
So instead of it printing "$34.09667" simply manipulating it to print "$34.10".
I was told to use something like this, assuming x is the variable I'm trying to print in dollars and cents.
cout << setprecision(2) << x << endl;
The variable I'm trying to convert is a double if it helps.
#include <iostream>
#include <iomanip>
usingnamespace std;
// This program is designed to calculate how much interest someone will accumulate given a certain amount of time.
// It will calculate interest accrued after 30 days, 180 days and 360 days.
// The interest rate for this program is 5%, although one could input any percentage.
// Enjoy.
double investment; // Declaring the interest percentage.
// The next three functions calculate the interest for the respective time periods.
void getInterest30(double x) {
double Interest = 0.05 / 365 * (investment);
double total30;
total30 = (x)+(30 * Interest);
cout << "The total interest accrued for 30 days is $";
cout << fixed << setprecision(2) << total30 << endl;
}
void getInterest180(double y) {
double Interest = 0.05 / 365 * (investment);
double total180;
total180 = (y) + (180 * Interest);
cout << "The total interest accrued for 180 days is $";
cout << fixed << setprecision(2) << total180 << endl;
}
void getInterest360(double z) {
double Interest = 0.05 / 365 * (investment);
double total360;
total360 = (z)+(360 * Interest);
cout << "The total interest accrued for 360 days is $";
cout << fixed << setprecision(2) << total360 << endl;
}
int main() {
cout << "Hey, user! I've been brushing up on my accounting skills!" << endl;
cout << "I'm going to calculate interest accrued on an investment of yours for three time periods." << endl;
cout << "But first, I'll need an investment amount. Would you enter one from zero to one hundred, please?" << endl;
cin >> investment;
while (investment > 100 || investment < 0) {
cout << "C'mon, user...work with me here! Read the instructions and enter another one." << endl;
cin >> investment;
}
getInterest30(investment); // Function call for the thirty day interest calculation.
getInterest180(investment); // Function call for the 180 day interest calculation.
getInterest360(investment); // Function call for the 360 day interest calculation.
return 0;
}