How do I call my functions from main?

closed account (S2wCM4Gy)
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
#include<iostream>
#include<iomanip>

using namespace std;

//Initialize variables because the homework said to. I do not however have to use them.

void initialize(int x, int y, char z)
{
x=0;
y=0;
z=' ';
}

//User will input values here. How do I input the values once I execute?

void getHoursRate(double hours, double rate)
{
	cout << "Please enter the number of hours worked: ";
	cin >> hours;
	cout << "\nPlease enter the rate per hour then press return. Example $17.50 = 17.50 :";
	cin >> rate;
}
//Calculating the value of the paycheck
double payCheck(double amount, double hours, double rate)
{
	getHoursRate(hours, rate);
	if (hours > 40)
	{
		amount = ((hours - 40) * (rate * 1.5)) + (40 * rate);
	}
	else
	{
		amount = rate * hours;
	}
	return amount;
}

//Printing the PayCheck out

void printCheck(double hours, double rate, double amount)
{
	payCheck(amount, hours, rate);
	cout << fixed << showpoint << setprecision(2);
	cout << "Hours worked: " << hours << "\nRate per hour: " << rate
		 << "\nPayCheck Salary: " << amount;
}

int main()
{
	void initialize();
	void getHoursRate();
	double payCheck();
	void printCheck();

//This is the part it skips to after a "successful" execution.

	system("pause");
	return 0;
}


I wrote out the above code for an assignment but I am unaware how to actually display the results/enter the values. I attempted to call the functions into the main but I clearly am not doing it correctly for the program merely runs then prompts for the return key to end. How do I add values to the getHoursRate functions and then print them using my printCheck function or where might I look to find the answers?
Last edited on
closed account (S2wCM4Gy)
Could you by chance add a section?
When you call void function in main function, you have to write down actual parameters. For example, in your first void function
1
2
3
4
5
6
void initialize(int x, int y, char z)
{
x=0;
y=0;
z=' ';
}

So when you calling, the form must be something like this: initialize(x, y, z); not necessary the same name, may be initialize(a, b, c);
Furthermore, you have to declare the variables in main function.
For example, with the above definition of your void func. In main function, there should be:
1
2
3
int x,y;
char z;
intialize(x, y, z);


Similarly, you should do like this for your next void functions when calling. Beside, notice about the variables, whether using value variable or reference variable. In this case, if you want to print the value of x, y, z after executing the above void function, you should declare like this:
void intialize(int& x, int& y, char& z)
Your should read more about function.
Last edited on
closed account (S2wCM4Gy)
Thank you very much for that explanation quan. I now understand what to do an it is working great. Thank you so much for your help.
Topic archived. No new replies allowed.