File read help

This was an assignment in class, already been graded on it, and I'm just trying to get info on how to fix my single issue.. I've already taken this to a couple tutors but they're completely worthless and were nearly clueless.. Anyway, here is the code..

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
44
45
46
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	//Initializing any variables, also making the program search for the text file needing to be read.
	ifstream inFile; //creaytes the fileobject for Intermediate24.txt
	inFile.open("Intermediate24.txt", ios::in); //ios::in allows the program to read the textfile
	int payCode = 0;//user input for paycode, also stores the paycode to display its salary
	int salary = 0;//stores the salary

	if(inFile.is_open())
	{//declares the start of the loop, If the text file is open, do the loop
		cout << "Welcome to the SALARY tab\nPlease search the payroll code"
			<< " that is associated with the salary you wish to view (1-32)"
			<< " or press -1 to end the program." << endl;
		cin >> payCode;
		while(payCode != -1)
		{ //beginning of loop after user input.. As long as paycode isn't -1
			if(payCode >= 1 && payCode < 33)
			{ //nested if structure, if paycode is between 1-32
			inFile >> payCode; //read the payCode entered by user
			inFile.ignore(1); //ignore the character AFTER the paycode
			inFile >> salary;//reads the salary
			cout << payCode << " " << salary; //displays the input payCode and its salary
			cout << " Please enter a paycode associated with a salary (1-32)" //continues the loop
				<< " or -1 to end the program.";
			cin >> payCode;
			}
			if(payCode <= 0 || payCode > 32){//tells user if paycode isn't within 1-32, end program
				cout << "Invalid paycode, ending program" << endl;
				payCode = -1;
			}
			inFile.close(); //closes the text file
		cout << endl;
	}
	}
	else
	{ //displays error if file doesn't open or IS not open
		cout << "File is not open" << endl;
	}


	}


The textfile to the program is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 


My issue is this; When I enter the payCode 1, it should display the salary associated with 1, well, it does, but then if I enter payCode 2, it'll show salary for 1.. No matter what number I start with, all it shows is the salary for 1 and it frustrates me.. Sample output without all the cout statements

1
2
3
4
Enter payCode: 1 :: Salary is 27200
Enter payCode: 10 :: Salary is 27200
Enter payCode: 32 :: Salary is 27200
Enter payCode: 33(or -1) :: Program is now ending *This part excites me:D*
Last edited on
bump*
No ideas?
I see the error, I don´t know how to fix it.

inFile >> payCode; //read the payCode entered by user

inFile is your object used to read from the file. So it enters the file and looks for something to fit payCode (wich is an int). I guess after line 23 you coud do

cout << payCode << endl;

and you should see a 1, regardless of the user previous input. Why? cause you told the program to look for an int in the file and put it in a variable called payCode. It did, it went there and found it. It´s a 1. It was the first thing there.
After the user enters the paycode, you proceed to read the first line of the file (paycode 1). You then output this as the data the user requested; i.e., you are always outputting the first line of the data. You need to check what paycode the user entered and read lines until they match.

I think the repeating data issue comes from you closing the file then on any following loops you attempt to read from it; this fails, so the salary data is left unchanged and you print it out.
I guess you could put the program to read every thing with a Loop until payCode maches inFile. There has to be another way? If the list was really long, the program would be rather inefficient.
1
2
3
4
5
6
7
8
9
10
11
12
while(payCode != -1)
...
			inFile >> CODEsearch; //I created this here
                        while (CODEsearch != payCode){

                         /* something for the program to keep reading */

                        }
			inFile.ignore(payCode); //ignore the character AFTER the paycode
			inFile >> salary;//reads the salary
			cout << payCode << " " << salary; //displays the input payCode and its salary
			
You could stop looking after you read a paycode higher than the one you are looking for (since the entries in the file are sorted). There isn't really any other way to read the entries other than a linear search of each entry since each one is of variable length and some could be missing.

One idea would be to load the entire file data into an associate array that maps paycodes to salaries. This would require some time to load the entire file, but from there any lookup would be relatively fast.
Topic archived. No new replies allowed.