I am working on another assignment that gets a user to input an employee code in two parts (letter and number) using two variables. It then opens a file and checks it for the characters entered and displays the proper lines. It has no errors, but it only wants to display the last line in the file. Also, my if statement keeps saying that the p# codes are invalid. I have spent at least 6 hours on this so far and can't get it to work. Any help or suggestions would be greatly appreciated.
This is what I have so far:
//Ch13AppE02.cpp
//Displays the names of salespeople having
//a specific code entered by the user; the codes and
//names are stored in a sequential access file
//Created/revised by <your name> on <current date>
#include <iostream>
#include <string>
#include <algorithm>
#include <fstream>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::ifstream;
using std::ios;
//variable declaration
void displayCode();
int main()
{
string code= "";
string codenum= "";
//Enter a code to be searched
cout << "Enter an employee code to be searched: "<< endl;
cin>>code;
cout << "Enter an employee number code to be searched: "<< endl;
cin>>codenum;
//call appropriate function
//or display an error message
while (code !="x" && codenum !="x")
{
if (code =="p" && codenum =="1"|| code =="p" && codenum =="2")
displayCode();
if (code =="f" && codenum =="1"|| code =="f" && codenum =="2")
displayCode();
else
cout<<"You have entered an incorrect code. Please enter f1, f2, p1 or p2." << endl;
return 0;
}//end while
}// end of main function
void displayCode()
{
//reads records from a file
//and then displays them
string code= "";
string codenum= "";
//open the file
ifstream inFile;
inFile.open("namecode.txt", ios::in);
//determine if the file is open
if (inFile.is_open())
{
//read a record
inFile >> code>>codenum;
}//end if
while (!inFile.eof())
{
//read another record
getline(inFile, code);
getline(inFile, codenum);
inFile >> code>>codenum;
}//end while
if (inFile.eof()){
inFile.close();
}
//display the code and code number
cout << "The code is: " << code <<codenum<< endl;
}//end of displayCode function