The code works as it is until I try to put it into functions.
So, with everything in the main() function, it works.
This is a project for class and while I understand the concept..I'm not sure exactly what to do.
When I'm done, I shouldn't have any variables declared globally.
Any advice?
Thanks.
By the way, I tried to comment as much as I could incase there was alot of "wth, why'd you do that"s lol.
Code: Select all
//NecrixWolf
//2010
//The purpose of this program is to demonstrate compounding interest in a
//"bank account"
//This program is meant to receive input from the user:
//How much money to begin with
//Which month to begin with
//What rate of interest will be applied to each month
//How many months should be calculated
//The following should be the output:
//The current month
//The amount of interest earned this month
//The new(total) balance
//This will be looped up to the number of months
//Three functions are used to achieve this: input, calculate, and output
#include <iostream.h>
#include <string.h>
#include <iomanip.h> //To use for isdigit() error handling later
#include <ctype.h>
using namespace std;
//Function prototypes
void get_input();
void calculate();
void output(int count);
//Global variables
float rate;
double money, inter, total = 0;
bool error = false; //To be used later for error handling..maybe.
//To convert counter value into month output for the user
string one = "January",
two = "February",
three = "March",
four = "April",
five = "May",
six = "June",
seven = "July",
eight = "August",
nine = "September",
ten = "October",
eleven = "November",
twelve = "December";
//Begin main function
int main()
{
get_input();
for (int count = start; count <= (months +1); count++)
{
calculate();
output(count);
}
system("PAUSE");
return 0;
}
//Input function gets user's input
void get_input()
{
cout << "How much money to start?: $";
cin >> money;
cout << "How many months are you going to be calculating?: ";
cin >> months;
cout << "What month are you starting at?: ";
cin >> start; //Uses "start" variable instead of "count" for code readability
cout << "What is the interest rate for each month?: %";
cin >> rate;
cout << '\n';
rate = rate / 100; //Divides given rate by 100 so the user can enter
//rate of interest in percent form
}
//Calculates..duh
void calculate()
{
inter = money * rate; //Calculates amount of interest earned for current month
money = money * (1 + rate); //Calculates the amount of money for the month before
//interest is added
total += money; //Finds the total for each month
}
//Guess what this one does..
void output(int count)
{
cout << "For the month of: ";
//Uses a switch to "convert" the counter's value into the month for output
switch(count)
{
case 1: cout << one;
break;
case 2: cout << two;
break;
case 3: cout << three;
break;
case 4: cout << four;
break;
case 5: cout << five;
break;
case 6: cout << six;
break;
case 7: cout << seven;
break;
case 8: cout << eight;
break;
case 9: cout << nine;
break;
case 10: cout << ten;
break;
case 11: cout << eleven;
break;
case 12: cout << twelve;
break;
}
cout.setf(ios::fixed);
cout << "\nYour interest is: $" << setprecision(2) << inter;
cout << "\nYour new balance is: $" << setprecision(2) << total << "\n\n";
}