Issue with Return totalhour;?????

I'm writing a Payroll Calculator for my Intro class, and I've gotten majority of the kinks worked out. We aren't allowed to use any global variables, and it has to be written entirely through functions...

In my getHours() function, I used a while-loop (the rubric requires this) to count the hours worked over the course of 5 days. For some reason, it isn't returning the correct number to the rest of the program despite the fact when I put

cout << totalhour;

between the end of the while loop and the return statement, the program output's the correct number that I need...

Could anyone give me a tip on how to fix this?

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
105
106
107
108
109
110
111
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

//Function Prototypes
void displayInstructions();
float getRate();
float getHours();
float calculateMath(float payRate, float totalhour);
void displayResults(string name, float grosspay, float totalWithholdings, float netPay);


//Main Function
int main()
{
  displayInstructions();

  return 0;
}


//Function Definitions

//Function 1
void displayInstructions()
{
  string name;
  float payRate;
  float totalhour, grosspay, totalWithholdings, netPay;
  cout << "Enter the name of the employee: ";
  cin >> name;

  if (name != "done")
    {
      getRate();
      getHours();
      calculateMath(payRate, totalhour);
      displayResults(name, grosspay, totalWithholdings, netPay);
    }
  else
    {
      cout << "Bye!" << endl;
    }

}

//Tax Rate
float getRate()
{
  float payRate;
  cout << "Enter the hourly pay rate: ";
  cin >> payRate;
  return payRate;
}

//Hours Worked
float getHours()
{
  int x = 1;
  float hour;
  float totalhour = 0;

  while (x <= 5)
    {
      cout << "Enter the number of hours worked for Day " << x << ": ";
      cin >> hour;
      totalhour = totalhour + hour;
      x = x + 1;
    }
  return totalhour;
}

//Calculate Gross Pay, Withholdings, and Net Pay
float calculateMath(float payRate, float totalhour)
{
  float grosspay;
  float withholdingsTax, totalWithholdings, netPay;
  int state = .0125;
  int fica = .0765;
  int federal;
  grosspay = payRate * totalhours;
  if (grosspay < 500)
    {
      federal = .15;
    }
  else
    {
      federal = .25;
    }
  withholdingsTax = state + fica + federal;
  totalWithholdings = grosspay / withholdingsTax;
  netPay = grosspay - totalWithholdings;
  return grosspay, totalWithholdings, netPay;
}

//Displaying Results
void displayResults(string name, float grosspay, float totalWithholdings, float netPay)
{
  cout << setprecision(2) << fixed;
  cout << left;
  cout << endl;
  cout << "Payroll" << endl;
  cout << "======================================" << endl;
  cout << endl;
  cout << setw(25) << "Employee Name:" << name << endl;
  cout << setw(25) << "Gross Pay:" << "$" << grosspay << endl;
  cout << setw(25) << "Total Withholding:" << "$" << totalWithholdings << endl;
  cout << setw(25) << "Net Pay:" << "$" << netPay << endl;
}
Look at this snippet:
1
2
3
4
  if (name != "done")
    {
      getRate();
      getHours();


Both of these functions return values why aren't you using the returned values? You may want to review your documentation about the purpose of return values from functions.

http://www.cplusplus.com/doc/tutorial/functions/

I'm confused by what you mean about this....perhaps my professor didn't do such a great job teaching this to me.

Would you be able to explain?
Did you read the link I provided, it should thoroughly explain the issue.
From what I understand (theres a VERY good chance I'm wrong in this), I should be using

payRate = getRate();
totalhour = getHours();

instead of what I have on those lines?

Like I said, I'm in a very basic intro class, pretty obvious by the fact I can't get a function to work properly :(

EDIT: I tried what I typed above, no change so obviously I wasn't correct. Could I be having this issue because I used a float for totalhour instead of an int?
Last edited on
I tried what I typed above, no change so obviously I wasn't correct.

You'll need to post more code, the complete program would probably be best.


You can see the changes I made to lines 37 and 38, but no change in the execution.


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
105
106
107
108
109
110
111
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

//Function Prototypes
void displayInstructions();
float getRate();
float getHours();
float calculateMath(float payRate, float totalhour);
void displayResults(string name, float grosspay, float totalWithholdings, float netPay);


//Main Function
int main()
{
  displayInstructions();

  return 0;
}


//Function Definitions

//Function 1
void displayInstructions()
{
  string name;
  float payRate;
  float totalhour, grosspay, totalWithholdings, netPay;
  cout << "Enter the name of the employee: ";
  cin >> name;

  if (name != "done")
    {
      payRate = getRate();
      totalhour = getHours();
      calculateMath(payRate, totalhour);
      displayResults(name, grosspay, totalWithholdings, netPay);
    }
  else
    {
      cout << "Bye!" << endl;
    }

}

//Tax Rate
float getRate()
{
  float payRate;
  cout << "Enter the hourly pay rate: ";
  cin >> payRate;
  return payRate;
}

//Hours Worked
float getHours()
{
  int x = 1;
  float hour;
  float totalhour = 0;

  while (x <= 5)
    {
      cout << "Enter the number of hours worked for Day " << x << ": ";
      cin >> hour;
      totalhour = totalhour + hour;
      x = x + 1;
    }
  return totalhour;
}

//Calculate Gross Pay, Withholdings, and Net Pay
float calculateMath(float payRate, float totalhour)
{
  float grosspay;
  float withholdingsTax, totalWithholdings, netPay;
  int state = .0125;
  int fica = .0765;
  int federal;
  grosspay = payRate * totalhour;
  if (grosspay < 500)
    {
      federal = .15;
    }
  else
    {
      federal = .25;
    }
  withholdingsTax = state + fica + federal;
  totalWithholdings = grosspay / withholdingsTax;
  netPay = grosspay - totalWithholdings;
  return grosspay, totalWithholdings, netPay;
}

//Displaying Results
void displayResults(string name, float grosspay, float totalWithholdings, float netPay)
{
  cout << setprecision(2) << fixed;
  cout << left;
  cout << endl;
  cout << "Payroll" << endl;
  cout << "======================================" << endl;
  cout << endl;
  cout << setw(25) << "Employee Name:" << name << endl;
  cout << setw(25) << "Gross Pay:" << "$" << grosspay << endl;
  cout << setw(25) << "Total Withholding:" << "$" << totalWithholdings << endl;
  cout << setw(25) << "Net Pay:" << "$" << netPay << endl;
}
No there is a change, you just can't see it because you still doing the same thing wrong with another of your functions. Plus you can only return one item from a function using the return statement, not two or even three.
I figured out my problems, thank you for the help!
Last edited on
Topic archived. No new replies allowed.