Void Function issues

Hey,
So I'm doing an assignment(worth less than 1% of my grade) and I'm stuck on void functions. We had to write a program and then part 2 was to create certain void functions using my original code. I've posted the original code and the void code that I've started working on so far. I really have no clue what I'm doing, and seem to be doing basic void programs with cout right, but am having issues with cin. This is a really beginner intro class, so I had no knowledge of anything code related 2 months ago.

Original code; this works fine and I have no issues with it.
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
#include <iostream>		//for cin and cout
#include <iomanip>		//for setprecision
#include <string>

using namespace std;

// The prupose of this program is to ask for a user to input a loan amout and interest rate percentage. The program then calculates the mothly payments
// and the total interest to be paid as well as the total amout left to be paid. Finally the program determines the average annual interest payemnts
// and the average annual interest rate.

int main()
{
	//Declare Variables
	float loan, loan1, intrate, monthlypay, intpay, intrate2, totalint, yearint, actualint;
	int paymentnum;
	string another;

	//Welcome message
	cout<<"Welcome to Christopher's Bank\n\n";

	//Ask user to input loan amount and interest rate
	cout<<"What is the amount of your loan? ";
	cin>>loan;
	cout<<"What is the interest rate? (Please enter as a %) ";
	cin>>intrate;

	loan1 = loan;

	//display total loan amount and monthly payments and calculate monthly payments
	monthlypay = loan/20;
	cout<<"\nMonthly payment on a loan of $"<<fixed<<setprecision(2)<<loan<<" is $"<<fixed<<setprecision(2)<<monthlypay<<"\n\n";

	//calculate monthly payments and display them
	cout<<"Payment details:\n\n"
		<<"Payment Number	Interest Paid	Remaning Balance\n"
		<<"--------------	-------------	----------------\n";
	paymentnum = 0;
	intrate2 = intrate / 100;
	totalint=0;
	yearint=0;

	while(loan > 0)
	{
		intpay = loan * intrate2 / 12;
		loan = loan + intpay - monthlypay;
		paymentnum++;
		if(loan<=0)
		{
			loan=0;
		}
		cout<<"	"<<paymentnum<<"		"<<intpay<<"		"<<loan<<endl;
		totalint= totalint + intpay;
	};

	//calcuate total interest paid for one year, the total interest and the actual interest rate

	cout<<"\nTotal interest paid $"<<totalint<<endl;

	yearint= totalint / paymentnum * 12;
	cout<<"\n\nInterest paid for one year: $"<<yearint<<endl;

	actualint = yearint / loan1 * 100;
	cout<<"actual interest rate: "<<actualint<<endl;

	//ask the user if they ahve another loan to calculate
	cout<<"Do you have another loan to evaluate? (Y or y for yes, any other input for no) ";
	cin>>another;
		if(another == "Y" || another == "y")
		{
			return main();
		}
		else
		{
			cout<<"It was a pleasure doing business with you.\n"
				<<"Anytime you need a loan think of <Christopher's Bank>\n\n";
			system("pause");
			return(0);
		}
}


With Void Functions. I need Void functions called: welcome() for my welcome message(it works the way it is), header() for the payment header(it works the way it is), and my 2 issues: getuserinput() and actualinterestrate().
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
#include <iostream>		//for cin and cout
#include <iomanip>		//for setprecision
#include <string>

using namespace std;

// The prupose of this program is to ask for a user to input a loan amout and interest rate percentage. The program then calculates the mothly payments
// and the total interest to be paid as well as the total amout left to be paid. Finally the program determines the average annual interest payemnts
// and the average annual interest rate. THis program is the same as the previous program except it uses void functions.

//create void functions
void welcome()
{
	cout<<"Welcome to Christopher's Bank\n\n";
};

void header()
{
	cout<<"Payment details:\n\n"
	<<"Payment Number	Interest Paid	Remaning Balance\n"
	<<"--------------	-------------	----------------\n";
};

void getuserinput()
{
	float loan, intrate;
	cout<<"What is the amount of your loan? ";
	cin>>loan;
	cout<<"What is the interest rate? (Please enter as a %) ";
	cin>>intrate;
};
void actualinterestrate()
{
	float actualint, yearint, loan1;
	actualint = yearint / loan1 * 100;
	cout<<"actual interest rate: "<<actualint<<endl;
};

int main()
{
	//Declare Variables
	float loan, loan1, intrate, monthlypay, intpay, intrate2, totalint, yearint, actualint;
	int paymentnum;
	string another;

	//Welcome message
	welcome();

	//Ask user to input loan amount and interest rate
	getuserinput();

	loan1 = loan;

	//display total loan amount and monthly payments and calculate monthly payments
	monthlypay = loan/20;
	cout<<"\nMonthly payment on a loan of $"<<fixed<<setprecision(2)<<loan<<" is $"<<fixed<<setprecision(2)<<monthlypay<<"\n\n";

	//calculate monthly payments and display them
	header();
	paymentnum = 0;
	intrate2 = intrate / 100;
	totalint=0;
	yearint=0;

	while(loan > 0)
	{
		intpay = loan * intrate2 / 12;
		loan = loan + intpay - monthlypay;
		paymentnum++;
		if(loan<=0)
		{
			loan=0;
		}
		cout<<"	"<<paymentnum<<"		"<<intpay<<"		"<<loan<<endl;
		totalint= totalint + intpay;
	};

	//calcuate total interest paid for one year, the total interest and the actual interest rate

	cout<<"\nTotal interest paid $"<<totalint<<endl;

	yearint= totalint / paymentnum * 12;
	cout<<"\n\nInterest paid for one year: $"<<yearint<<endl;

	actualinterestrate();


	//ask the user if they ahve another loan to calculate
	cout<<"Do you have another loan to evaluate? (Y or y for yes, any other input for no) ";
	cin>>another;
		if(another == "Y" || another == "y")
		{
			return main();
		}
		else
		{
			cout<<"It was a pleasure doing business with you.\n"
				<<"Anytime you need a loan think of <Christopher's Bank>\n\n";
			system("pause");
			return(0);
		}


}
actualinterestrate() is invalid. You never filled in the floats. Rewrite this function to use parameters.
My entire program freezes right after I pass getuserinput(). I'm really not sure how to write the functions without essentially rewriting the entire code inside of each function since the void functions seem to work independently of anything else that's happening in the program. In theory, after I use getuserinput() the program should spit out a whole bunch of numbers. Run the original program that I posted and you'll see what I mean. The second program just jams after getuserinput() and has no output after that point.
I don't know if you're still there, but you should learn about something called scope. You're constantly failing to remember the scope of variable.
Basically, every piece of your code(functions) is using other data then your main function, but you're treating it as it was the same data.
Read about global variables and scope. Understand them and you will see the error in your code. Your data defined in main function isn't global. You can't treat it like it was.

Cheers!
While what MatthewRock says is true, don't be tempted to use globals to share data between functions. Instead, you want to go back to your textbook and learn how to pass arguments into functions, and back from functions into the calling code.

Globals are extremely problematic, and using them is a very bad habit to get into.
Topic archived. No new replies allowed.