2012-05-06 2 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 # 012050000000000000 # 01000000000000 # 10500 # 552,314,960,453,210 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 
작동