2012-05-06 12 views
0

現在、プログラムの問題が発生しています。私は現在、ユーザーに2つのオプションを与えるプログラムを実行しています。オプション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#00500
20#65000
21#65500
22#70200
23#71000
24#71100
25#72000
30#83000
31#84000
32#90000

答えて

2

この1つは

#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 
作品
関連する問題