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
#
70#20 83000
31#84000
32#90000

4

1 回答 1

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
于 2012-05-06T04:52:42.187 回答