Finding yearly interest out of compound interest

When I try to find out my yearly interest, I get the wrong answer. The first year calculation works just fine but for the next year interest doesn't print out the right answer or goes off by 0.01. What is the matter?

// Compound Interest
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void get_user_input(float &principal, float &interest_rate, int &first_year, int &first_month, int &total_months)
{
cout << "Enter the starting principal: ";
cin >> principal;
cout << "Enter the current interest rate (Ex. 0.09 for 9%): ";
cin >> interest_rate;
do
{
cout << "Enter the first year: ";
cin >> first_year;
if ((first_year < 1900) || (first_year > 2050))
{
cout << "Invalid year.\n";
cin.clear();
cin.ignore(100, '\n');
}
} while ((first_year < 1900) || (first_year > 2050));

do
{
cout << "Enter the first month (1 for Jan., 2 for Feb., etc...): ";
cin >> first_month;
if ((first_month < 1) || (first_month > 12))
{
cout << "Invalid month.\n";
cin.clear();
cin.ignore(80, '\n');
}
} while ((first_month < 1) || (first_month > 12));

cout << "Enter the total number of months: ";
cin >> total_months;
cin.ignore(80, '\n');
}

void print_date(int year, int month)
{
switch (month)
{
case 1:
cout << "Jan";
break;
case 2:
cout << "Feb";
break;
case 3:
cout << "mar";
break;
case 4:
cout << "Apr";
break;
case 5:
cout << "May";
break;
case 6:
cout << "Jun";
break;
case 7:
cout << "Jul";
break;
case 8:
cout << "Aug";
break;
case 9:
cout << "Sep";
break;
case 10:
cout << "Oct";
break;
case 11:
cout << "Nov";
break;
case 12:
cout << "Dec";
break;
}
cout << " " << year << ": ";
}

void calculate_principal(float &principal, float interest_rate)
{
principal = principal + (principal * (interest_rate / 12));
}
void interest(float first_principal, float principal, float &interest_earned)
{
interest_earned = principal - first_principal;
}

int main()
{
float first_principal, next_principal = 0.0 , principal, interest_rate, interest_earned, total_interest = 0.0, year_interest = 0.0;
int first_year, first_month, total_months, month_count, current_year, current_month;
char WaitTemp[2];

get_user_input(principal, interest_rate, first_year, first_month, total_months);


current_year = first_year;
current_month = first_month;
first_principal = principal;
next_principal = principal;


cout.setf(ios::fixed);
cout.setf(ios::showpoint);
for (month_count = 0; month_count < total_months; month_count++)
{
if (current_month != first_month)
{
calculate_principal(principal, interest_rate);
first_month = 0;
}
interest(next_principal, principal, interest_earned);
print_date(current_year, current_month);
cout << "$" << setprecision(2) << principal << " with the $" << setprecision(2) << interest_earned << " earned this month. \n";

if (current_month < 12)
{
current_month++;
next_principal = principal;
total_interest += interest_earned;
year_interest += interest_earned;
}


else
{
total_interest += interest_earned;
year_interest += interest_earned;
cout << "The yearly interest earned in is $" << year_interest << endl;
cout << "\nPress Enter to Continue... \n";
cin.get(WaitTemp, 0);
cin.ignore(80, '\n');
current_month = 1;
current_year++;
next_principal = principal;
first_principal = principal;
year_interest *= 0;
cin.clear();
cin.ignore(80, '\n');
}
}
cout << "The total interest that you earned in " << month_count << "months is/are : $" << total_interest << endl;
return 0;
}
Last edited on
Use [code][/code] tags when you post code (it's the <> format icon on the right).
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

void get_user_input(float &principal, float &interest_rate, int &first_year,
                    int &first_month, int &total_months)
{
  cout << "Enter the starting principal: ";
  cin >> principal;
  cout << "Enter the current interest rate (Ex. 0.09 for 9%): ";
  cin >> interest_rate;
  do {
    cout << "Enter the first year: ";
    cin >> first_year;
    if ((first_year < 1900) || (first_year > 2050)) {
      cout << "Invalid year.\n";
      cin.clear();
      cin.ignore(100, '\n');
    }
  } while ((first_year < 1900) || (first_year > 2050));

  do {
    cout << "Enter the first month (1 for Jan., 2 for Feb., etc...): ";
    cin >> first_month;
    if ((first_month < 1) || (first_month > 12)) {
      cout << "Invalid month.\n";
      cin.clear();
      cin.ignore(80, '\n');
    }
  } while ((first_month < 1) || (first_month > 12));

  cout << "Enter the total number of months: ";
  cin >> total_months;
  cin.ignore(80, '\n');
}

void print_date(int year, int month)
{
  switch (month) {
  case 1:
    cout << "Jan";
    break;
  case 2:
    cout << "Feb";
    break;
  case 3:
    cout << "mar";
    break;
  case 4:
    cout << "Apr";
    break;
  case 5:
    cout << "May";
    break;
  case 6:
    cout << "Jun";
    break;
  case 7:
    cout << "Jul";
    break;
  case 8:
    cout << "Aug";
    break;
  case 9:
    cout << "Sep";
    break;
  case 10:
    cout << "Oct";
    break;
  case 11:
    cout << "Nov";
    break;
  case 12:
    cout << "Dec";
    break;
  }
  cout << " " << year << ": ";
}

void calculate_principal(float &principal, float interest_rate)
{
  principal = principal + (principal * (interest_rate / 12));
}

void interest(float first_principal, float principal, float &interest_earned)
{
  interest_earned = principal - first_principal;
}

int main()
{
  float first_principal, next_principal =
      0.0, principal, interest_rate, interest_earned, total_interest =
      0.0, year_interest = 0.0;
  int first_year, first_month, total_months, month_count, current_year,
      current_month;
  char WaitTemp[2];

  get_user_input(principal, interest_rate, first_year, first_month,
                 total_months);


  current_year = first_year;
  current_month = first_month;
  first_principal = principal;
  next_principal = principal;


  cout.setf(ios::fixed);
  cout.setf(ios::showpoint);
  for (month_count = 0; month_count < total_months; month_count++) {
    if (current_month != first_month) {
      calculate_principal(principal, interest_rate);
      first_month = 0;
    }
    interest(next_principal, principal, interest_earned);
    print_date(current_year, current_month);
    cout << "$" << setprecision(2) << principal << " with the $" <<
        setprecision(2) << interest_earned << " earned this month. \n";
    if (current_month < 12) {
      current_month++;
      next_principal = principal;
      total_interest += interest_earned;
      year_interest += interest_earned;
    }
    else {
      total_interest += interest_earned;
      year_interest += interest_earned;
      cout << "The yearly interest earned in is $" << year_interest << endl;
      cout << "\nPress Enter to Continue... \n";
      cin.get(WaitTemp, 0);
      cin.ignore(80, '\n');
      current_month = 1;
      current_year++;
      next_principal = principal;
      first_principal = principal;
      year_interest *= 0;
      cin.clear();
      cin.ignore(80, '\n');
    }
  }
  cout << "The total interest that you earned in " << month_count <<
      "months is/are : $" << total_interest << endl;
  return 0;
}


You might want to look at line 138.
Topic archived. No new replies allowed.