|
-
November 13th, 2010, 11:39 PM
#1
C++ Loop problem
Here's what I have, but I'm having a problem because it's displaying a chart with employee id # and wage right after each employee instead of at the end.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int NUM_EMPLOYEES = 7;
const int STRING_SIZE = 8;
int hours[NUM_EMPLOYEES];
int payRate[NUM_EMPLOYEES];
float wages[NUM_EMPLOYEES];
cout << "Enter the requested information for each employee. " << endl;
cout << endl;
for (int count = 0; count < NUM_EMPLOYEES; count++)
{
// Array with the 7 employee identification numbers.
char empId[NUM_EMPLOYEES][STRING_SIZE] =
{ "5658845", "4520125", "7895122", "8777541",
"8451277", "1302850", "7580489" };
cout << "Employee #" << empId[count] << endl;
cout << '\t' << "Hours worked: " << " ";
cin >> hours[count];
while( hours[count] < 0 ) // Hours worked must be at least 0.
{
// Error message.
cout << "Hours worked must be 0 or more. Please re-enter: " << " ";
cin >> hours[count];
}
cout << '\t' << "Pay rate: $" << " ";
cin >> payRate[count];
while( payRate[count] < 6 ) // Pay rate must be at least $6.
{
// Error message.
cout << "Pay rate must be $6.00 or more. Please re-enter: " << " ";
cin >> payRate[count];
}
{
wages[count] = hours[count] * payRate[count]; // Calculates employees wages.
cout << "----------------------------" << endl;
cout << "Employee" << '\t' << "Wages" << endl;
cout << "----------------------------" << endl;
cout << "Employee #" << empId[count] << " " << "$ " << wages[count] << endl;
}
}
return 0;
}
But I want it to display like this...
Employee #5658845
Hours worked: -1
Hours worked must be 0 or more. Please re-enter: 6
Pay rate: $5
Pay rate must be 6.00 or more. Please re-enter: $6
Employee #4520125
Hours worked: 7
Pay rate: $7
Employee #7895122
Hours worked: 8
Pay rate: $8
Employee #8777541
Hours worked: 9
Pay rate: $9
Employee #8451277
Hours worked: 10
Pay rate: $10
Employee #1302850
Hours worked: 11
Pay rate: $11
Employee #7580489
Hours worked: 12
Pay rate: $12
----------------------------
Employee Wages
----------------------------
Employee #5658845 $ 36.00
Employee #4520125 $ 49.00
Employee #7895122 $ 64.00
Employee #8777541 $ 81.00
Employee #8451277 $ 100.00
Employee #1302850 $ 121.00
Employee #7580489 $ 144.00
Press any key to continue . . .
-
November 15th, 2010, 02:29 PM
#2
Re: C++ Loop problem
Try making a second for loop for the output, something similar to the following...
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const int NUM_EMPLOYEES = 7;
const int STRING_SIZE = 8;
int hours[NUM_EMPLOYEES];
int payRate[NUM_EMPLOYEES];
float wages[NUM_EMPLOYEES];
cout << "Enter the requested information for each employee. " << endl;
cout << endl;
for (int count = 0; count < NUM_EMPLOYEES; count++)
{
// Array with the 7 employee identification numbers.
char empId[NUM_EMPLOYEES][STRING_SIZE] =
{ "5658845", "4520125", "7895122", "8777541",
"8451277", "1302850", "7580489" };
cout << "Employee #" << empId[count] << endl;
cout << '\t' << "Hours worked: " << " ";
cin >> hours[count];
while( hours[count] < 0 ) // Hours worked must be at least 0.
{
// Error message.
cout << "Hours worked must be 0 or more. Please re-enter: " << " ";
cin >> hours[count];
}
cout << '\t' << "Pay rate: $" << " ";
cin >> payRate[count];
while( payRate[count] < 6 ) // Pay rate must be at least $6.
{
// Error message.
cout << "Pay rate must be $6.00 or more. Please re-enter: " << " ";
cin >> payRate[count];
}
{
wages[count] = hours[count] * payRate[count]; // Calculates employees wages.
}
} //end of first for loop
cout << "----------------------------" << endl;
cout << "Employee" << '\t' << "Wages" << endl;
cout << "----------------------------" << endl;
for (count = 0; count < NUM_EMPLOYEES; count++) //second for loop
{
cout << "Employee #" << empId[count] << " " << "$ " << wages[count] << endl;
}
return 0;
}
-
November 15th, 2010, 06:58 PM
#3
Re: C++ Loop problem
The above suggestion by jeron is the easiest method to create the output you want. You also have a more basic issue in the need to keep the association between the employee number and other employee data, in this case to create output. You can do this by creating an employee class that has the members int employeeNumber, and float currentPay. You can save the pay in the current pay float, and then output the employee number and any other data stored in the employee object. This creates a container (object) that holds all of the data for a given employee.
A simpler way would be to load the two values into a pair of vectors. First declare both vectors and populate the vector of your employee numbers.
Code:
std::vector<int> employeeNumber;
std::vector<float> currentPay;
employeeNumber.push_back(5658845);
employeeNumber.push_back(4520125);
employeeNumber.push_back(7895122);
employeeNumber.push_back(8777541);
employeeNumber.push_back(8451277);
employeeNumber.push_back(1302850);
employeeNumber.push_back(7580489);
Then process through the loop of employees to calculate their pay and store it in the currentPay vector. Then you just have to loop through both vectors to create the final output.
Code:
for(int i=0; i<employeeNumber.size(); i++) {
std::cout << "Employee # " << employeeNumber[i] << " $" << currentPay[i] << std::endl;
}
I didn't bother to code up the loop for entering the data and calculating pay, since it is pretty much what you already have. Just store the calculated value in the currentPay vector , like,
currentPay.push_back(tempPay);
and then print both vectors in a loop at the end.
Either way, I think you will find it expedient to make use of more of the features of cpp, such as the non-primitive data types (vector, user defined class, etc.). Employees are a natural fit for a data structure like a class, so that would be a good way to go.
Let me know if you want me to code this more completely or want more information on creating a class to contain the data.
LMHmedchem
Last edited by LMHmedchem; November 15th, 2010 at 07:02 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|