2012-04-09 6 views
0

私が取り組んでいる課題に問題があります。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文をスキップし表示されますレコードを追加するには、1をヒットした場合、次のよう

コードがあります。

これはなぜ起こるのでしょうか?

+0

が見え、我々はCINにそれらを変更>>、それが働きました。 – Dani

答えて

0

おそらくバッファの問題があるので、標準入力をフラッシュする必要があります。詳細はgoogleをチェックしてください。

+0

完璧な、それは働いた!ありがとう。 – Dani

+0

答えをマークしてくださいので、質問は終了します。 – Samson

2

あなたは1

を押した後、あなたはあなたの入力ストリーム内の改行文字を残しているが、あなたが入力しているキーのすべてを考えてみましょう。あなたは、ADDRECのために987、名前「ジョン」の後、従業員IDを「1」と入力した場合、あなたの入力ストリームは次のようになります。

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'); 
関連する問題