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!
#include <iostream>
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 = (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