Loop Help.

This is my first time writing a loop for a program and I cannot get an output I want so that my following function works. I have to write a loop that reads the amount of hours an employee works each day for five days. I am able to run the loop, have the inputs put in, and it gives a total. However, when I go to test the gross payment, it says that the total gross pay is "Inf". I don't really know how to fix it, even after all the research I did.

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <iomanip>

using namespace std;
string readEmployeesName()
{
  //Declarations

  string employeesName;

  //Intializations
  //Input

  cout << "Please enter the employee's name: ";
  cin >> employeesName;

  //Output
  //Prologue

  return employeesName;
}

double readHourlyPayRate()
{
  //Declaration

  double hourlyPayRate;

  //Intializations
  //Input

  cout << "Please enter the hourly pay rate of employee: ";
  cin >> hourlyPayRate;

  //Process/Calculations
  //Output
  //Prologue

  return hourlyPayRate;
}

double readHoursWorked()
{
  //Declaration

  double hoursWorked;
  double employeesName;
  double days;
  double totalHoursWorked;

  //Intializations
  //Input

  while(days <= 5)
    {
      cout << "Please enter the amount of hours worked by employee on day" << days << ":" <<  endl;
      cin >> hoursWorked;

      days++;

      totalHoursWorked = totalHoursWorked+hoursWorked;      
    }

  //Process/Calculations
  //Output
  //Prologue

  return totalHoursWorked;
}

double calculateGrossPay()
{
  //Declaration

  double grossPay;
  double totalHoursWorked;
  double hourlyPayRate;

  //Initializations

  grossPay = 0.0;

  //Input
  //Process/Calculations

  grossPay = hourlyPayRate*totalHoursWorked;

  //Output

  cout << "Total Gross Pay: " << grossPay << endl;

  //Prologue

  return grossPay;
}

int main()
{
  //Declarations

  string employeesName;
  double hourlyPayRate;
  double hours;
  double hoursWorked;
  double grossPay;
  double stateTax;
  double federalTax;
  double FICA;
  double totalWithholding;
  double netPay;

  //Initializations

  hourlyPayRate = 0.0;
  hours = 0.0;
  hoursWorked = 0.0;
  grossPay = 0.0;
  stateTax = 0.0;
  federalTax = 0.0;
  FICA = 0.0;
  totalWithholding = 0.0;
  netPay = 0.0;

  //Input

  employeesName = readEmployeesName();
  while (employeesName!="done")
    {
      hourlyPayRate = readHourlyPayRate();
      hoursWorked = readHoursWorked();

      cout << "In main before calculateGrossPay: " << grossPay << endl;
      grossPay = calculateGrossPay();
      cout << "In main after calculateGrossPay: " << grossPay << endl;

      stateTax = calculateStateTax();
      federalTax = calculateFederalTax();
      FICA = calculateFICA();
      totalWithholding = calculateTotalWithholding();
      netPay = calculateNetPay();
      displayResults();
      employeesName = readEmployeesName();
    }

  //Output
  //Prologue

  return 0;
}
grossPay is declared locally inside calculateGrossPay(), so you cant cout it in main().

try cout<<calculateGrossPay, I think this is what you want.

Otherwise, you could declare grossPay globally and change calulateGrossPay to a void function which assigns grossPay inside.
Well, you can re-write calculateGrossPay() to take two values, one for hourlyPayRate and one for hoursWorked.
Topic archived. No new replies allowed.