// goal: Demonstrate use of vector from the STL // This program stores, in two vectors, the hours worked by several // employees, and their hourly pay rates. #include #include #include // Needed to define vectors using namespace std; int main() { vector hours; // hours is an empty vector vector payRate; // payRate is an empty vector int numEmployees; // The number of employees int index; // Loop counter cout << "is the vector hours empty? "; if (hours.empty()) cout << "Yes! \n"; else cout << "NO. \n"; // Get the number of employees. cout << "How many employees do you have? "; cin >> numEmployees; // Input the payroll data. cout << "Enter the hours worked by " << numEmployees; cout << " employees and their hourly rates.\n"; for (index = 0; index < numEmployees; index++) { int tempHours; // To hold the number of hours entered double tempRate; // To hold the payrate entered cout << "Hours worked by employee #" << (index + 1); cout << ": "; cin >> tempHours; hours.push_back(tempHours); // Add an element to hours cout << "Hourly pay rate for employee #"; cout << (index + 1) << ": "; cin >> tempRate; payRate.push_back(tempRate); // Add an element to payRate } // Display each employee's gross pay. cout << "Here is the gross pay for each employee:\n"; cout << fixed << showpoint << setprecision(2); for (index = 0; index < hours.size(); index++) { // the .at function returns the value at location index // it works the same as [ ] however it cannot modify the value // and it does check for "out of bounds" error double grossPay = hours[index] * payRate.at(index); cout << "Employee #" << (index + 1); cout << ": $" << grossPay << endl; } // the .size function tells the number of elts in a vectore cout << "the number of elts in vector hours is: " << hours.size() << ". "; if (hours.empty()) cout << " The array is empty.\n"; else cout << "The array is not empty. \n"; return 0; }