c ++代码跳过get line语句的问题

时间:2012-04-09 20:04:25

标签: c++

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

代码如下:

#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语句是员工编号。

有谁知道为什么会这样?

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

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