Okay, so for an assignment in my c++ programming class, we needed to create a class based employee database. This program will allow the user to input the data for 10 employees. This program will then list all of the employees with their pay.
I decided to use a for loop to iterate through an array of objects of the "employee" class. Each iteration allows you to enter the Name, Job Title, Hourly Wage, and Hours Worked for a single employee, and then iterates again in order to allow the user to populate the rest of the array with information.
The problem that I'm having occurs right after the first iteration of the populating for loop. After the first iteration, it always skips over the "Enter Employee name: " Field, and goes straight into "Enter Job Title: ". I'm not exactly sure why, and I have heard some information about it being an issue with the cin buffer? I'm not exactly sure. Appreciate anyone who can help me out, as this is due tonight!
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class employee{
private:
float hoursWorked;
float hourlyWage;
float pay = (hoursWorked * hourlyWage);
string jobTitle;
string name;
public:
void Set_name(string usrInput1){
name = usrInput1;
}
void Set_jobTitle(string usrInput2){
jobTitle = usrInput2;
}
void Set_hoursWorked(float usrInput3){
hoursWorked = usrInput3;
}
void Set_hourlyWage(float usrInput4){
hourlyWage = usrInput4;
}
float Get_pay(){
return pay;
}
string Get_name(){
return name;
}
string Get_jobTitle(){
return jobTitle;
}
float Get_hoursWorked(){
return hoursWorked;
}
float Get_hourlyWage(){
return hourlyWage;
}
};
int main(){
int employeeNum = 10;
employee employees[10];
for (int i = 0; i < 10; i++){
string empName;
string empJobTitle;
float empHourlyWage;
float empHoursWorked;
cout << "Enter employee " << i+1 << " name: "; // This line is skipped when the program iterates any time past the first.
getline(cin, empName);
employees[i].Set_name(empName);
cout << "\n";
cout << "Employee " << i+1 << " Job Title: ";
cin >> empJobTitle;
employees[i].Set_jobTitle(empJobTitle);
cout << "\n";
cout << "Employee " << i+1 << " Hourly Wage: ";
cin >> empHourlyWage;
employees[i].Set_hourlyWage(empHourlyWage);
cout << "\n";
cout << "Employee " << i+1 << " Hours Worked: ";
cin >> empHoursWorked;
employees[i].Set_hoursWorked(empHoursWorked);
cout << "\n";
}
for (int i = 0; i < 10; i++){
cout << "Employee " << (i+1) + ":" << "\n";
cout << "Employee Name: " << employees[i].Get_name();
cout << "Employee Job Title: " << employees[i].Get_jobTitle();
cout << "Employee Hourly Wage: " << employees[i].Get_hourlyWage();
cout << "Employee Hours Worked: " << employees[i].Get_hoursWorked();
cout << "Employee Pay: " << employees[i].Get_pay();
}
system("Pause");
return 0;
}