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

おそらくバッファの問題があるので、stdinをフラッシュする必要があります。詳細についてはグーグルをチェックしてください。

于 2012-04-09T20:14:52.547 に答える