Sequential Access Files

Hello All

I am currently working on a program that gathers a payroll code, and compares the entry of payroll code to a text file and then displays the appropriate payroll amount. The user is given a sentinel value of 0 when they want to end the program. I cannot get the program to terminate at the input 0. Additionally, the program only returns the first line of the text file, regardless of the payroll code entered. I have included the source code below, and the content of the text file to search and return the payroll amount. Hopefully a fresh set of eyes can help point out what step I missed to get the program to function completely. Feel free to point out any other errors that may exist in my code as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <string>
using namespace std;




int main()
{
	//declaring variables
	string payrollCode = "";
	string payrollAmount = "";

	//declaring ifstream file
	ifstream inputFile;

	//gathering initial user input
	cout << "Please enter a payroll code 1-32(0 to end)";
	getline(cin, payrollCode, '\n');

	while (payrollCode != "0" || payrollCode <= "32")
	{
		inputFile.open("PayrollCode.txt", ios::in);
		if (inputFile.is_open())
		{
			while(!inputFile.eof())
			{
			getline (inputFile, payrollCode, '#');
			getline (inputFile, payrollAmount);
			cout << "Salary $" << payrollAmount << endl << endl;
			inputFile.close();
			cout << "Please enter a payroll code 1-32(0 to end)";
			getline(cin, payrollCode, '\n');
			}//end while
		}
		else
		cout << "Error. File not found." << endl;
		//end if
	}//end while
	system ("pause");
	return 0;
}//end of the main function 



Access File:

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

I'm actually working on the same thing. I noticed you don't have any search code I've initialized;

string code, search;

I don't have mine working yet but I am pretty sure this string search is mandatory.

Topic archived. No new replies allowed.