搜索和匹配访问文件中的数据
Searching and Matching Data In Access File
我目前遇到了程序问题。我目前正在运行一个为用户提供两个选项的程序。选项 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
这个有效
#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
相关文章:
- C++数据文件、数组和计算赋值
- 数据文件的第二行未正确读取
- 在 C++/C 中使用 CURL 发出带有数据文件的 GET HTTP 请求
- 将数据文件读入结构 C++
- 引入流时C++数据文件未正确读取?
- 如何在数据文件中查找和显示内容
- 如何处理错误"E1696命令行错误:无法在Visual Studio 2017中打开元数据文件"mscorlib.dll"?
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 将数据文件读入对象数组时出现问题
- C :数据文件有错误:预期的无限制ID
- C 和 C++:带有错误"Expected unqualified-id"的数据文件
- 如何在C 中读取一系列数据文件
- 从数据文件中读取每2个字节,并在向量中进行比较
- 写入单个大数据文件或多个小文件:哪个更快?
- 在读取数据文件时,如何找到用户字符串输入的字谜?
- 保护外部数据文件免受未经授权的修改
- C 读取数据文件
- C 如何将数据文件读取到结构或向量以及返回结构或向量
- 具有挑战性的数据文件格式,需要将其读取为包含类对象的数组的VAR
- C 中的Google单元测试:如何编写持久数据文件