File read help

alistjazz (21)
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
alistjazz (21)
bump*
Registered users can post here. Sign in or register to post.