Program to calculate interest isn't working.

This program is designed to calculate interest but it gives me the initial investment three times in a row for some reason. Anything stick out to you guys? Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  #include <iostream>
using namespace 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 = (investment * 0.05) / 365;
	float total30;
	total30 = (x) + (30 * Interest);
	cout << "The total interest accrued for 30 days is $" << total30 << endl;
}

void getInterest180(double y) {
	double Interest = (investment * 0.05) / 365;
	float total180;
	total180 = (y) + (180 * Interest);
	cout << "The total interest accrued for 180 days is $" << total180 << endl;
}

void getInterest360(double z) {
	double Interest = (investment * 0.05) / 365;
	float total360;
	total360 = (z)+(360 * Interest);
	cout << "The total interest accrued for 360 days is $" << total360 << endl;
}


int main() {
	float investment;

	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;

}
In start of main you are creating local variable float investment that hides your global variable double investment.
Global investment is initialized to 0 because its in global scope and its a default value for a double. All of your functions are using global investment instead the one thats local to main function.
Just delete the line 34 and the program will work

If you want access global variable from main you can do it like this cin >> ::investment; but there are no point of that local investment anyway so you can just delete it
Last edited on
This is a good example of why global variables are bad practice.
Ahh gotcha, thank you all!
Oh, another thing.

He permitted us to use the <iomanip> library but I can't figure out how to print the dollar amount in dollars and cents "DD.CC".

I tried using
 
cout << setprecision(2) << total30 << endl;


But it printed in scientific notation...anything I'm doing wrong?
I'd suggest using just one function and pass the number of days as a parameter.
1
2
3
4
5
6
7
8
void getInterest(double investment, int days) 
{
    const double rate = 5.0;
	
    double annualInterest = investment * rate/100.0;
    double totalInterest  = annualInterest * days / 365.0;
    cout << "The total interest accrued for " << days << " days is $" << totalInterest << endl;
}
Topic archived. No new replies allowed.