Hello angchan,
Looking at your code I would create a vector of type "EmployeeList" where each element of the vector is a complete class with different information. So unless you are required to use a linked list the vector is the best way to go.
For the class either put it in a separate header file or at the beginning of the file before main after the
using namespace std;
that is best not to use. I would also put the functions of the class in a separate ".cpp" file and bring them all together at compile time. If you are not use to using separate files it is best to put everything above main or if you have a prototype for everything you can put the functions after main.
In the "openInputFile" function the while loop is nice, but what you shoule be doing is an if statement, error message, pause and exit the program because if the file name is wrong there is no reason to continue. If you do not provide the file names that are available then the user will have to leave the program to check the file name. Then again the while loop might work. This is the first time I have seen this approach and will have to test it.
It would also help to have the input file you are using or at least a good sample of the file. Knowing what the file looks lime will help to check how the file is read and how you using the information.
In main when you open the input file you need to check if it is open before you continue otherwise you could end up with nothing. Also your use off the open is incorrect. Something like:
1 2 3 4 5 6 7 8
|
Employee1.open("File Name"); // <--- or you can use a std::string variable.
if (!Employee1)
{
std::cout << "\n File " << "File Name" << " did not open" << std::endl; // <--- "File Name" was a variable.
std::this_thread::sleep_for(std::chrono::seconds(5)); // <--- Needs header files chrono" and "thread".
exit(1); // <--- No point to continue.
}
|
For the first while loop in main. A good idea, but could be a big problem. In the while condition you are reading the name, but in this way if the name has to parts separated by a space you will only get the first name leaving the last name in the input buffer to be read on the next pass of the loop. With information in the input buffer the next will start with the input buffer and try to put the last name into a numeric variable which will cause the stream to fail, the while loop to end and leave you with a mess and an unfinished read. Not seeing the input file I would more likely read the "id" in the while condition and maybe the wage, but I would put the name inside the loop and use "std::getline(Employee1, name);" to get the whole name I would also precede the getline with:
Employee1.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
to clear the input buffer.
One thing you have missed is to close your input streams when you are finished with them or at least just before 'return".
I will have to load this up and see if there is anything I have missed.
Hope that helps,
Andy