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