2012-05-06 39 views
0

我目前陷入了一個程序問題。我目前正在運行一個程序,給用戶兩個選項。選項1允許用戶輸入工資單代碼1-32。輸入工資單代碼後,我需要搜索訪問文件以查找匹配項。一旦確定了比賽,我需要處理薪資代碼和字符「#」,然後將剩餘數據顯示爲薪資金額。選項2允許用戶結束程序。我目前正在編譯該程序,並正在運行。但是,它只存儲來自文件第一行的數據。這裏是我需要搜索的源代碼和文件數據。有人可以幫助我獲得搜索功能嗎?任何幫助或額外的方向非常感謝。搜索並匹配訪問文件中的數據

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

//function prototypes 
void displayPayroll(); 

int main() 
{ 
    //declaring variables 
    int menuOption = 0; 
    do 
    { 
    //display menu and get option 
    cout << "1 To Enter Payroll Code" << endl << endl; 
    cout << "2 End the program" << endl << endl; 
    cin >> menuOption; 
    cin.ignore(100, '\n'); 
    cout << endl; 
    if (menuOption == 1) 
     displayPayroll(); 
    } while (menuOption != 2); 

    system("pause"); 
    return 0; 
}// end of the main function 


void displayPayroll() 
{ 
    //declaring variables 
    string payrollCode = ""; 
    string payrollCompare = ""; 
    double payrollAmount = 0.0; 

    //declaring the fileObject and opening the file 
    ifstream inPayroll; 
    inPayroll.open("Intermediate24.txt", ios::in); 

    //determine if the file was openend correctly 
    if(inPayroll.is_open()) 
    { 
     cout << "Please enter a payroll Code 1-32: "; 
     getline (cin, payrollCode); 
     if (payrollCode >= "1" && payrollCode <= "32") 
     { 
      getline(inPayroll, payrollCode, '#'); 
      inPayroll >> payrollAmount; 
      inPayroll.close(); 
      cout << "Salary $" << payrollAmount << endl << endl; 
     } 
     else 
      cout << "Incorrect payroll code." << endl << endl; 
     //end if 
    } 

    else 
     cout << "Error. File not found." << endl; 
    //end if 
} //end of displayPayroll function 

1#27200
2#15000
3#23000
4#12000
5#25500
6#18400
7#19500
8#32000
9#29000
10#16500
20#65000
21#65500
22#70200
23#71000
24#71100
25#72000
30#83000
31#84000
32#90000

回答

2

這一個工作

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

//function prototypes 
void displayPayroll(); 

int main() 
{ 
    //declaring variables 
    int menuOption = 0; 
    do 
    { 
     //display menu and get option 
     cout << "1 To Enter Payroll Code" << endl << endl; 
     cout << "2 End the program" << endl << endl; 
     cin >> menuOption; 
     cin.ignore(100, '\n'); 
     cout << endl; 
     if (menuOption == 1) 
      displayPayroll(); 
    } while (menuOption != 2); 

    system("pause"); 
    return 0; 
}// end of the main function 


void displayPayroll() 
{ 
    //declaring variables 
    string payrollCode = ""; 
    string payrollCompare = ""; 
    //double payrollAmount = 0.0; 

    //declaring the fileObject and opening the file 
    ifstream inPayroll; 
    inPayroll.open("Intermediate24.txt", ios::in); 

    //determine if the file was openend correctly 
    if(inPayroll.is_open()) 
    { 
     cout << "Please enter a payroll Code 1-32: "; 
     getline (cin, payrollCode); 
     if (payrollCode >= "1" && payrollCode <= "32") 
     { 
      string temp; 
      size_t p ; 
      do{ 
       inPayroll >> temp; 
       p = temp.find("#"); 
      }while(temp.substr(0, p) != payrollCode); 
      inPayroll.close(); 
      cout << "Salary $" << temp.substr(p + 1) << endl << endl; 
     } 
     else 
      cout << "Incorrect payroll code." << endl << endl; 
     //end if 
    } 

    else 
     cout << "Error. File not found." << endl; 
    //end if 
} //end of displayPayroll function 
相關問題