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..
#include <iostream>
#include <fstream>
usingnamespace 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;
}
}
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*