0

嘿伙计们,我正在处理的任务有问题。

代码如下:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

class payroll
{
private:
    char empnum[10];
    char empfirstname[150];
    char emplastname[150];
    char empsin[9];
    char empdob[10];
    char empphone [15];
    char empstreet[100];
    char empcity[60];
    char empprovince[150];
    char empcountry[200];
    char empstatus[50];       //Employee status. EX: Terminated, On leave, Active etc.
    int empsalaryei;
    int empsalaryfedtax;
    int empsalarycpp;
    int empsalarynet;
    int empsalarygross;

public: 
    void addrec(void);
    void modrec(void);
    void viewrec(void);
    void exit(void);
};

payroll rec;
payroll emp;
ifstream inFile1;
ifstream inFile2;
bool check = false;

int main()
{
    system("CLS");
    char ch;

    do
    {
        cout<<"1. Add an employee\n";
        cout<<"2. Modify employee record\n";
        cout<<"3. View employee record\n";
        cout<<"0. Exit\n";
        cout<<"Please choose an item: ";
        if (check == true)
        {
            system("CLS");
            ch=0;
        }
        else
            cin>>ch;

        switch(ch)
        {
            case '1':
                emp.addrec();
                break;

            case '2':
                emp.modrec();
                break;

            case '3':
                emp.viewrec();
                break;

            case '0':
                emp.exit();
                return 0;
        }
    }while(ch !=0);

    return 0;

}

void open_employee_info()               //function to open employee data file
{
    string filename1 = "employee-details.dat";        //define which file to open 

    inFile1.open(filename1.c_str());  //open our payroll file
    if(inFile1.fail())                                                       //if our file open fails....
    {
        cout << "\nSorry the file was not opened successfully"<< "\n Please check that the file does exist or create it" << endl;
        exit(1);
    }
    cout << "\n The file was opened successfully" << endl;
}

void open_payroll_info()               //function to open payroll salary information file
{
    string filename2 = "payroll-info.dat";          //define file name

    inFile2.open(filename2.c_str());
    if(inFile2.fail())
    {
        cout << "\nSorry the file was not opened successfully"<< "\n Please check that the file does exist or create it" << endl;
        exit(1);
    }
    cout << "\n The file was opened successfully" << endl;
}

void payroll::addrec(void)          //Record adding
{
    char userinputadd = ' ';

    cout << "\nPlease Enter the Employee number: ";
    gets(rec.empnum);
    cout << "\nPlease Enter the Employee's First Name: ";
    gets(rec.empfirstname);
    cout << "\nPlease Enter the Employee's Last Name: ";
    gets(rec.emplastname);
    cout << "\nPlease Enter the Employee's Date of Birth (mmddyyyy): ";
    gets(rec.empdob);
    cout << "\nPlease Enter the Employee's Social Insurance Number: ";
    gets(rec.empsin);
    cout << "\nPlease Enter the Employee's Phone Number: ";
    gets(rec.empphone);
    cout << "\nPlease Enter the Employee's Address: ";
    gets(rec.empstreet);
    cout << "\nPlease Enter the Employee's City: ";
    gets(rec.empcity);
    cout << "\nPlease Enter the Employee's Province: ";
    gets(rec.empprovince);
    cout << "\nPlease Enter the Employee's Country: ";
    gets(rec.empcountry);
    cout<<"\nPlease Enter the Employee's Status: ";
    gets(rec.empstatus);
    cout << "\nPlease Enter the Employee's Weekly Gross Salary: ";
    cin >> rec.empsalarygross;

    cout<<"Would you like to return to the main menu?(y), else the program will exit: ";
    cin>>userinputadd;
    if (userinputadd =='y' || userinputadd =='Y')
        main();
    else
        exit();
}

void payroll::modrec(void)          //Record Modification
{
    system("CLS");
    int empmodnum=0;
    check = false;
    char userinputmod = ' ';

    cout<<"\nEnter the employee number for the record you would like to modify: ";
    cin>>empmodnum;

    cout<<"Would you like to return to the main menu?(y), else the program will exit: ";
    cin>>userinputmod;
    if (userinputmod =='y' || userinputmod =='Y')
        main();
    else
        exit();
}

void payroll::viewrec(void)         //Record viewing
{
    system("CLS");
    check = false;
    char userinputview = ' ';

    cout<<"Would you like to return to the main menu?(y), else the program will exit: ";
    cin>>userinputview;
    if (userinputview =='y' || userinputview =='Y')
        main();
    else
        exit();

}

void payroll::exit(void)
{
    inFile1.close();
    inFile2.close();
    check = true;
}

因此,如果您在前面的菜单中按 1 添加记录,您会看到它跳过了第一个 cin 语句,即员工编号。

有谁知道为什么会发生这种情况?

4

2 回答 2

2

按 1 后,您将在输入流中留下换行符。

考虑您正在键入的所有键。如果您为 addrec 输入“1”,然后输入员工 ID 987,然后输入姓名“John”,您的输入流如下所示:

1, \n, 9, 8, 7, \n, J, o, h, n, \n

您用 读取第一个字符cin >> ch。您拥有的下一个输入语句是gets().

gets()正确读取直到下一个换行符的所有字符。由于您仍然有后面的换行符,'1'因此第一行将被读取为空白。

尝试将其添加到您的程序中:

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
于 2012-04-09T21:08:50.590 回答
0

您可能有缓冲区问题,因此您需要刷新标准输入。查看谷歌了解详情。

于 2012-04-09T20:14:52.547 回答