Need to read paycode from txt file then display salary

I am trying to write C++ code to get the salary amount associated with the payCode. The #include 's that I am using are <iostream>, <string>, and <fstream>. I can't figure out how to read/search the pay codes to display only the salary amount from the txt file. Here is how I have coded it and it keeps failing when I build.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
ifstream inPayCodeList;
short code;
short payCode[19];
int payAmount[19];
int sub = 0;

inPayCodeList.open("payCodeData.txt");
if(inPayCodeList.is_open())
{
//assign the codes and amounts to parallel arrays
for (short index = 0; index < 20; index +=1)
{
inPayCodeList >> payCode[index];
inPayCodeList >> payAmount[index];
}
//input the pay code you are searching for the amount
cout << "Enter the pay code that you are searching for: ";
cin >> code;

//locate the position if the pay code
while (sub < 20 && payCode[sub] != code)
sub+=1;
//end while

//if the pay code was located in payCode array,
//display the pay amount associated with the code
if (sub > 20)
{
cout << "The salary for " << payCode[sub] <<
"is " << payAmount[sub];
}
else
//if the pay code is not located, display invalid message
cout << "An invalid pay code has been entered." << endl;
//end if
}
return 0;
}

And this is the txt file I am to get the info from

payCodeData.txt
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
Last edited on
Topic archived. No new replies allowed.