基于简单类的员工数据库。第一次迭代运行正常,其余的跳过输入字段

时间:2016-03-28 00:55:02

标签: c++ class oop

好的,对于我的c ++编程类中的赋值,我们需要创建一个基于类的员工数据库。该程序将允许用户输入10名员工的数据。然后,该计划将列出所有员工的工资。

我决定使用for循环遍历" employee"的对象数组。类。每次迭代都允许您输入单个员工的姓名,职务,每小时工资和工作小时数,然后再次迭代以允许用户使用信息填充数组的其余部分。

我在填充for循环的第一次迭代后发生的问题。在第一次迭代之后,它总是跳过"输入员工姓名:"现场,直接进入"输入职位名称:"。我不确定为什么,我听说过有关cin缓冲区问题的一些信息?我不太确定。感谢任何可以帮助我的人,因为这是今晚到期的!

#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;
}

0 个答案:

没有答案