Call Function problems?

I am new at c++ so I don't know what I'm missing. The program just asks for the pay rate, wage, and hours worked. However it does not print the inputted values in format, it just prints 0.00 0.00 0.00. It might be that I'm using the function call wrong or something like that, I was following the instructions in the function tutorial.

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
// Program reads an employee's hourly pay rate and the number of hours worked and displays the //wage
#include <iostream>
#include <iomanip>
using namespace std;

int getData(float hoursWorked, float payRate, float wage)
{
	cout << "What is your pay rate?" << endl;
	cin >> payRate;
	cout << "What is your wage?" << endl;
	cin >> wage;
	cout << "How many hours do you work?" << endl;
	cin >> hoursWorked;
	return wage, payRate, hoursWorked;

}
int main()
{
	float  hoursWorked = 0, payRate = 0, wage = 0;
	int z = getData(payRate, wage, hoursWorked);
	cout << fixed << showpoint;

	/* WRITE code to call function getData */

	wage = hoursWorked * payRate;
	cout << setw(10) << hoursWorked
		<< setw(10) << payRate
		<< setw(10) << wage << endl;

	system("pause");
	return 0;
}
read the warnings carefully at compile time.
return wage, payRate, hoursWorked;

They'd tell you that "wage" and "payRate" are actually being ignored here. That statement is equivalent to
return hoursWorked;

You can confirm this if you'd cout the value for "z" in main() -- z is set to whatever is entered for hoursWorked.

To return multiple items, you'd either need to package them up into an object, or turn your parameters into references (latter option you could make the function void).

Edit: also be consistent with the order of your parameters. You've changed the order like 3-4 times across your function calls, cin order, and cout order.
Last edited on
Also realized that you don't actually need to take wage as third parameter -- this is calculated.

Here are examples for the two possibilities I mentioned -- [1] returns a struct containing the three pieces, and [2] writes to the object references passed in from main as parameters:
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;

struct MyData
{
    MyData(float pay, float hours) : 
        pay(pay), 
        hours(hours),
        wage(pay*hours)
    {
    }
    float pay;
    float hours;
    float wage;
};
    

// 1. Single object encapsulating pieces of data
MyData GetDataBunched()
{
    float pay, hours;
    cout << "What is your pay rate?" << endl;
    cin >> pay; 
    cout << "How many hours do you work?" << endl;
    cin >> hours;
    return  MyData(pay,hours);
}

// 2. References
void GetDataRefs(float& pay, float& hours, float& wage)
{
    cout << "What is your pay rate?" << endl;
    cin >> pay;
    cout << "How many hours do you work?" << endl;
    cin >> hours;
    wage = pay*hours;   
}

int main()
{
    cout << fixed << showpoint;

    cout << "[1] GetData returned as a single object:\n";
    auto m = GetDataBunched();
    cout << setw(10) << m.hours
        << setw(10) << m.pay
        << setw(10) << m.wage << endl << endl;
        
    cout << "[2] GetData using references:\n";
    float pay, hours, wage;
    GetDataRefs(pay, hours, wage);
    cout << setw(10) << hours
        << setw(10) << pay
        << setw(10) << wage << endl;


    return 0;
}
Thank you so much for your help! You have no idea how much worry you saved me! Thank you.
Topic archived. No new replies allowed.