In my main function, the user inputs an ID number as a string variable which is then passed to the function above. I have tested multiple ID numbers that are in the txt file, and all of them return fail. So, I am assuming this is an issue of the function never reaching the “found = true” statement, but I cannot figure out why. The txt file just has ID numbers and names in the following format:
123
John Smith
etc.
string lookupEmployee(string idNum)
{
ifstream file;
string line, employeeName, fail = "Employee not found. ";
bool found = false;
file.open("Employees.txt");
if (file.fail())
{
cout << "File failed to open. " << endl;
exit(1);
return "-1";
}
while (!file.eof())
{
while (getline(file, line))
{
if (line == idNum)
{
getline(file, employeeName);
found = true;
break;
}
}
}
file.close();
if (!found)
{
return fail;
}
else
{
return employeeName;
}
}
chicknburrito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.