never ending loop

write a program that reads info about the employee, the number of hours are read in a loop and their sum is stored in the structure as the number of hours worked during the week, computes gross pay, tax deduction, netpay and total grosspay and tax deduction of the company.
My program goes thru a never ending loop, does anyone know why ?
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
Header:
struct Date
{ 
    int month;
    int day;
    int year;
};

struct EmployeeInfo
{
int id;
string firstname;
string lastname;
Date birthday;
Date datehired;
float payrate;
int hours;
};
sourcefile:

#include <iostream>
#include <string>
using namespace std;
#include "Employeeinfo.h"
int main()
{
EmployeeInfo employee;
double grosspay, tax, netpay, totalgp=0, totaltax=0, totalhours=0;
while(employee.hours != -99)
{
cin  >>  employee.id  >> employee.firstname  >>  employee.lastname >> employee.birthday.month>>employee.birthday.day>>employee.birthday.year>>employee.datehired.month>>employee.datehired.day>>employee.datehired.year>>employee.payrate>>employee.hours;
totalhours +=employee.hours;
grosspay = totalhours*employee.payrate;
if(grosspay>= 1000)
	tax = .20*grosspay;
else if(grosspay>=800 && grosspay<1000)
	tax = .18*grosspay;
else if(grosspay>=600 && grosspay<800)
	tax = .15*grosspay;
else
	tax = .10*grosspay;
netpay = grosspay - tax;
totalgp+= grosspay;
totaltax+= tax;
cout << "Name: "  << employee.lastname + "," + employee.firstname << endl;
       cout <<"ID : "<<employee.id << endl;
       
       cout << "DOB: "<<employee.birthday.month << "/"
		     <<employee.birthday.day << "/"
		     <<employee.birthday.year << endl;
       cout << "BOH: "<<employee.datehired.month << "/"
		      <<employee.datehired.day << "/"
		      <<employee.datehired.year << endl;
       cout <<"Hours:  "<<totalhours << endl;
       cout <<"Pay Rate: $"<<employee.hours << endl;
       cout <<"Gross Pay: $"<<grosspay << endl; 
       cout <<"Tax: $"<<tax << endl;
       cout <<"Netpay: $"<<netpay << endl;
       cout <<"Total gross pay: $"<<totalgp<<endl;
       cout << "Total tax deduction: $" <<totaltax<<endl;
       cout << endl << endl;  
}
cout<< "Total grosspay =$"<<totalgp<<endl;
cout<<"Total tax=$"<<totaltax<<endl;
return 0;
}
-------------------------------------------------------------
Input:
58243 John Doe 10 25 1981 6 15 2005 8.75 7 4 7 5 8 -99
51423 Alex Nunez 11 13 1988 4 13 2003 9.25 6 4 5 7 4 -99
54278 Chelsea Garcia 8 7 1990 5 25 2004 8.50 5 6 3 6 7 -99
59315 Rick Stevens 5 4 1991 4 5 2003 9.00 8 7 5 6 4 -99
58126 George Wayde 8 6 1989 5 7 2001 10.25 4 5 7 8 6 -99
I count 11 input 'cin's, but your input has more values. You should put a breakpoint on the line after cin, run in debug mode and see what the values of your employee object are. You'll quickly be able to see the value of employee.hours, and you'll know why it doesn't stop!

Cheers
you mean the cin inputs (except for employee.hours) to be before the while loop ?
why is no one helping ;/
Bump
He means that line 13 reads 11 values from the input, but the input file has 16 values on each line. So the first time through the loop it reads the 11 values, which means that employee.hours does NOT get set to -99 because it hasn't read that yet. The next time through the loop it reads the 12th value of the first line into the employee ID, the 13th value into the first name etc. When it gets to the last name on the second line, the read fails and you're hosed from there on.

Here are the first two records output. Notice that the second record is all messed up.
$ ./foo < foo.in | head -40
Name: Doe,John
ID : 58243
DOB: 10/25/1981
BOH: 6/15/2005
Hours:  7
Pay Rate: $7
Gross Pay: $61.25
Tax: $6.125
Netpay: $55.125
Total gross pay: $61.25
Total tax deduction: $6.125


Name: 5,7
ID : 4
DOB: 8/-99/51423
BOH: 0/15/2005
Hours:  14
Pay Rate: $7
Gross Pay: $122.5
Tax: $12.25
Netpay: $110.25
Total gross pay: $183.75
Total tax deduction: $18.375

I know what he was saying I was just trying to understand what he meant when he told me i should put a breakpoint on the line after the cin.
bump
He meant to run the program in a debugger. Do you know what a debugger is? It's a way to run a program and tell the computer to stop at specific places. Then you can examine the variables etc. He's saying to stop execution at line 14 by putting a "breakpoint" there. That tells the debugger to stop the program at that location. Then you should examine the contents of the employee variable.

Hope this helps.
bump adding a breakpoint to line 14 didn't help and i don't know how to debug... Anyone know what's wrong with the sourcefile can anyone see the reason for the non ending loop ?
Maybe its becuase, You tell it while(employee.hours != -99)

You tell it, as long as employee.hours is not equal to 99, run this loop.
So once you enter the loop. employee.hours never changes. employee.hours never becomes -99. And the loop will run forever until employee.hours = 99;
the sentinel value is entered at the end of each input.
Have you tried a do-while loop?
professor told us not to use those.
Why are you using structs intead of classes?
it was part of our assignment "number of hours are read in a loop and their sum is stored in the structure as the number of hours worked during the week"
Last edited on
Do you have skype, maybe I can help you out there easier.
yes find me at skill2232
Topic archived. No new replies allowed.