Part 2:
When using "std::string" in a program DO NOT accept that "string EmployeeName[MAXSIZE];" is OK just because the IDE and compiler do not flagg it as an error. When you get to either "std::cin >> EmployeeName[idx]" or "std::cout << EmployeeName[idx]" you need the string header file so these 2 functions will know how to process the variable.
As nice as global variables are for ease access any line of code that follows them can change their value and it can be time consuming to track down where it went wrong. Consider making them a "static" public variable of the class. Or define them in "main" and pass them to the functions that need them.
You have used "std::ios::app" and "std::ios::ate" in your program. The both do the same thing, but "std::ios::ate" works with "std::ios::trunc", "app" does not.
Using "std::ios::app" with an "ifstream", if it not ignored by the compiler then it would put the file pointer at the ens and your first read would set the (eof) bit. Not what you want.
When you open a file stream for input or output you need to check that the file is open and usable. The best way is:
1 2 3 4 5
|
if (!inFile)
{
return std::cout << "\n\n File " << std::quoted(inFileName) << " did not open.\n", 1; // <--- Requires header file "<iomanip>".
//return std::cout << "\n\n File \"" << inFileName << "\" did not open.\n", 1;
}
|
Line 38, defining this variable as a "char" would work better because at line 72 you have
while (cont == "Yes" || cont == "yes" || cont == "Y" || cont == "y");
. But what about "YES", "yeS", "yES" or something like "Yes more". Of course being formatted input it would stop at the 1st space and leave "more" in the buffer for whatever comes next for input. This could be a problem. Using a string for input you could check:
while (cont[0] == 'Y' || cont[0] == 'y');
From line 42 - 68 notice that I ended the prompt with "text: ". The space after the colon and lack of a (\n) or "endl" puts the input on the same line as the prompt.
I have no idea what the input would be for the variables defined as "std::string"s, but consider using "std::getline()" for these in case there may be a space in the input. Formatted input like
std::cin >> EmployeeName[idx];
will stop at the first space it encounters and leave what may be left in the buffer for the next input which will through the rest of the input off and if you try to input a non numeric character into a numeric variable the "cin" would be in a failed state and unusable the rest of the program.
In the "Search" function:
In the for loop
for (int t = 0; t < sizeof(EmployeeID); t++)
. Do you realize what the value of "sizeof(EmployeeID)" will return? "EmployeeID" is an "int" that would be 4 bytes and also an array of 100, so 4 * 100 = 400 bytes. Not what you are looking for.
|
sizeof(EmployeeID) / sizeof(EmployeeID[0])
|
would give you a size of 100 or you could just "MAXSIZE", but that is not what you want. The variable "n", a better name would help like "amtOfArrayUsed" or "amt Used" even "count" might work if the line "using namespace std;" does not cause a problem.
The if statement looks good at the moment, but I have not tested it yet. When the if statement becomes true you should end the block with "break;" to break out of the for loop because there is no point in going any farther.
In the next for loop I rearranged the "output" to make it easier to read. I noticed that you could make your life easier by setting up the output as:
1 2 3 4 5 6 7 8 9 10 11 12
|
for (int w = 0; w < s; w++)
{
outfile
<< "\n"
<< EmployeeID[w] << " "
<< EmployeeName[w] << ","
<< EmployeeDateOfBirth[w] << ','
<< EmployeeAge[w] << " "
<< EmployeeGender[w] << ","
<< EmployeeQualification[w] << ","
<< EmployeeSalary[w]; // <--- I think this should end with a new line?
}
|
By adding a comma at the end of the strings you can use "std::getline" with the 3rd parameter set to (',') to read the stings.
In "main" you do not need a "cout" and "endl" for each line. Here the 8 line of quoted strings are considered 1 large string and the white space from the end of 1 string to the beginning of the next string is ignored by the compiler. The advantage here is that it looks much closer to what will be sent to the screen and also easier to edit, add, subtact a line or edit.
Your if/else if statement could easily be turned into a switch/case if you have studied that.
If I have missed something let me know.
Andy