user defined functions

Currently working on a program for my intro to C++ class, using user defined functions. It is still in the works, so not a finished product by far. I plan on use do-while loops to exclude any user inputs that are not of the correct type, and repeat until the user enters the right variable type, but really want a look at the code I used for my user function, and suggestions. It took me about an hour to figure out how to get the variables right for this function. Basically it takes user input for initial investment, interest rate and years, then the user function is supposed to do all the calculations.

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
//Program in the works

#include <iostream>
#include <iomanip>
#include <limits>
#include <math.h>

using namespace std;

double compoundAnnual(double x,double y, double z); //set prototype
 
int main() // main source code
{
	double startValue, interestRate, years, product, principal; // declare variables
	int i, x=1; //declare variables
	
	
	cout << "Welcome user. This program is written to calculate the principal on an investment account, based on your input. \n";
	cout << "Enter the starting value of the investment" << endl;
	cin >> startValue;
	cout << "Enter the interest rate: for example 5% would be 5: \n" << endl;
	cin >> interestRate;
	cout << "Enter the term of the investment: " <<endl;
	cin >> years;
	
	for (i=0; i <= years; i++) // calculates loop position
	{
		while (x <= years) //while x < years loop will run to provide the principal of an investment
		{
		cout << "Value of investment is: $" << compoundAnnual (startValue, interestRate, x) << endl; // returns the value of investment at a given year, based on x, until x = the years of the investment
		++x;
		;
		}
	}
	return 0;
}
double compoundAnnual (double startValue, double interestRate, double years) // declares variables for user defined function
{
	double principal = pow ( (startValue*(1+interestRate/100)), years); // calculates the value of principal, and then returns the value as the value for the function compoundAnnual
	return principal;
} 
Last edited on
I would use a more expressive name instead of 'x', something like "currentYear" :S
Also you don't need to declere 'i' outside the loop and especially not so far away from it.

Wouldn't this produce the same output?

1
2
for (int currentYear = 1; currentYear <= years; currentYear++) 
    cout << "Value of investment is: $" << compoundAnnual(startValue, interestRate, currentYear ) << endl; 


So you get to remove 2 variables from the top.
Also, notice your function takes a double for year, yet your x is an int, I would change one of the two to match and avoid debugging future truncation problems :S

also this:
1
2
3
4
double compoundAnnual(double startValue, double interestRate, double years) 
{
	return pow((startValue*(1 + interestRate / 100)), years); 
}

you don't need to store 'principal ' inside a function when you can just return it (since you're not doing anything else with it), I think it resoult in less variables construction though I can't be sure of it.

For the function checking input validity, watch on <cctype> header, also you want to check the cin methods called peek(),ignore(),fail(),clear()
Last edited on
Cool thanks for the feedback. I was struggling figuring this out. The text book didn’t give very good examples. I’ll take the suggestions into the code. I’ve also used fail, ignore, and clear before to prevent the wrong inputs from going through the program, just need to incorporate it into this code. I mainly built this to test it, and too see if it was working. Seems like I might be getting really crazy numbers though, so I’ll have to look into that
Topic archived. No new replies allowed.