issue with validation loop

Hi, my assignment is as follows:


Write a program that uses the following arrays:

• empId: an array of seven long integers to hold employee identification numbers.
The array should be initialized with the following numbers:

5658845 4520125 7895122 8777541
8451277 1302850 7580489

hours: an array of seven integers to hold the number of hours worked by each employee
payRate: an array of seven doubles to hold each employee’s hourly pay rate
wages: an array of seven doubles to hold each employee’s gross wages

The program should relate the data in each array through the subscripts. For exam-
ple, the number in element 0 of the hours array should be the number of hours
worked by the employee whose identification number is stored in element 0 of the
empId array. That same employee’s pay rate should be stored in element 0 of the
payRate array.


The program should display each employee number and ask the user to enter that
employee’s hours and pay rate. It should then calculate the gross wages for that
employee (hours times pay rate) and store them in the wages array. After the data has
been entered for all the employees, the program should display each employee’s identification number and gross wages.


Input Validation: Do not accept negative values for hours or numbers less than 6.00
for pay rate.



My program is working fine, apart from my validation loop when accepting input for my arrays. When I enter a bad input the program recognizes the error and outputs my corresponding error message; however, it fails to loop and moves into the next step. I can't quite figure out why it catches the bad input, but fails to loop and ask for the input again

Any help is greatly appreciated



My code is as follows:


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
#include <iostream>
#include <string>

using namespace std;

int main()
{

	//declarations
	int empId[] = {
		5658845, 4520125, 7895122, 8777541,
		8451277, 1302850, 7580489
	}; 

	int hours[7] = { 0 };
	double payRate[7] = { 0 };
	double wages[7] = { 0 };
	

	//for loop to ask for and accept input for payRate and hours
	for (int i = 0; i < 7; i++)
	{

		do 
		{
			//prompt for user input
			cout << "Please Enter the hours worked by employee " << empId[i] << ": " << endl;
			cin >> hours[i];

			if (cin.fail() || hours[i] < 0)
			{
				
				
				cout << "The number of hours worked must be 0 or higher.\n";
				
				
				//reset variable to 0
				hours[i] = 0;
				


				if (cin.fail())
				{
					cin.clear();
				}

				// flush cin's buffer 
				cin.ignore(100, '\n');


				//output prompt again
				//cout << "Please Enter the hours worked by employee " << empId[i] << ": " << endl;
				//cin >> hours[i];
			}

		} while (hours[i] = 0);

		
		do {
			//prompt for user input
			cout << "Please Enter the payrate of employee " << empId[i] << ": " << endl;
			cin >> payRate[i];


			if (cin.fail() || payRate[i] < 6.00)
			{
				cout << "An employees pay rate cannot be less than 6.00 \n\n";

				//reset variable to 0
				payRate[i] = 0;


				if (cin.fail())
				{
					cin.clear();
				}

				// flush cin's buffer 
				cin.ignore(100, '\n');
			}

		} while (payRate[i] = 0);

		

	} //end of the for loop






	cout << "Employee Gross Pay \n" << "" << endl;


	//for loop to output each employees gross wages
	for (int i = 0; i < 7; i++)
	{
		//calculate each employees gross wages 
		wages[i] = hours[i] * payRate[i];
		
		
		cout << "| " << empId[i] << ": $" << wages[i] << " | " << endl;

	}
	


	
	system("pause");
	return 0;
}
Last edited on
Your do while loop conditions are wrong - you're just missing an = for the comparison.

It should be:

while (hours[i] == 0);

and

while (payRate[i] == 0);

Topic archived. No new replies allowed.