2012-04-09 25 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声明是员工编号。

有没有人有一个想法,为什么会发生?

+0

看起来就像是用得声明一个问题,我们改变了他们对CIN >>和它的工作。 – Dani 2012-04-09 20:09:11

回答

0

你可能有一个缓冲区问题,所以你需要刷新标准输入。查看谷歌的详细信息。

+0

完美,工作!谢谢。 – Dani 2012-04-10 16:14:35

+0

请标记答案,以便问题关闭。 – Samson 2012-04-11 08:51:25

2

你离开换行符在你的输入流中按1

考虑所有你键入的按键后。如果您对addrec键入“1”,那么987雇员ID,然后命名为“约翰”,你的输入流是这样的:

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');