Hello kmheflin712,
I agree with
jlb the program would benefit immensely from the use of a struct and "std::vector. Using a struct would condense most of the arrays down to just one, but a vector would work better than an array.
For the most part you are goo down to the point where you ask the user for a file name. except for
using namespace std; // <--- Best not to use.
and I would add
constexpr size_t MAXSIZE{ 10 };
then when you define your arrays
std::string lname[MAXSIZE];
. This way if you need to change the size of your arrays it only needs done in one place and not the eight + arrays that you have.
You are asking a lot of the user to remember the file name to use when you ask for the file name. It would be nice to list the file names that can be used so the user can see what there is to choose from. On method I have been recently working with used the "<experimental/filesystem>" header file and the other uses "regex" to list the files. The "regex" method makes a nicer output and can limit what is displayed on the screen, but it will take some modification first.
IMHO I do not know what "din" and "dout" means to you, but for these streams I like to use "inFile" and "outFile". I have found that when reading through code it take less thought and work to understand what is being used. Just a thought. You can call your streams anything that you want.
Unless your compiler is pre2011 standards, from C++11 on the use of ".c_str()" is not needed. If you have to use ".c_str()" to open a file then you should consider upgrading the IDE/compiler or maybe you may be able to adjust the IDE to use the C++11 standards.
The if statement dealing with a file that did not open is OK, but again without listing the file names that can be used you could end up in an endless loop trying to find the correct file name. For an if statement like this I usually use:
1 2 3 4 5 6
|
if (!inFile)
{
std::cout << "\n File \"" << filename << "\" did not open" << std::endl;
return 1; //exit(1); // If not in "main".
}
|
Instead of getting th file name wrong a second time you leave the program until you have the correct file name.
whitenite1 wrote:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
din >> fname[i]; // Why??
while (!din.eof())
{
din >> fname[i];
din >> paytype[i];
din >> rate[i];
din >> ytdGross[i];
i++; // Why increase i BEFORE assigning lname[i]??
// fname and lname won't match up anymore PLUS you'll be out-of-bounds in array
// after 10th name
din >> lname[i];
}
|
|
I can see why he is confused. On line 1 I believe yo meant to use "lname" instead of "fname". By inputting "fname" twice you will leave the first element of "lname" blank.
Usually a while loop with the while condition based on "eof" is the wrong way to read a file, but you have managed to use it correctly.
The more often method to read a file is:
1 2 3 4 5 6 7 8 9 10 11 12
|
while (din >> lname[i])
{
din >> fname[i];
din >> paytype[i];
din >> rate[i];
din >> ytdGross[i];
// Or
//din >> ytdGross[i++];
i++;
}
|
I would suggest using "std::getline(din, lname, ',') and the same for "fname" because you may fine a first or last name that has a space in it and thet would through the whole read section off.
Your input file could end up looking something like this:
lname,fname,payType rate ytdGross
|
Now if "lname" or "fname" has a space in the name it will read correctly. After you read "ytdGross" you will need the line
din.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
This will clear the input buffer before the next "std::getline" is used.
That is as far as I have tested so far.
Looking over the switch/case, cases "1" and "2" should call a function not contain the program. The idea of "main" and the case statements sre to direct the flow of the program not be the program. You have demonstrated that you can use functions. Make more use of them.
Also your for loops:
for (int i = 0; i < 10; i++)
would work better as:
for (int i = 0; i < MAXSIZE; i++)
, but even that is wrong.
You assume that the input file will always have ten records, but it may not. It could have less than ten or more. This is where a "vector" is useful.
Another thing I noticed:
if (paytype[i] == "S")
. What is the value of "i" here? I found nowhere where you reset the value of "i" before you use it in the case statement. If you did input ten records then "i" would equal ten and this would put you trying to access the array past the last element boundary and this will not work. It will create a run time error.
You created a variable "numOfEmp", but never use it. When you read the file "numOfEmp" would be a better choice than "i". At this time the variable is never used, so I am not sure yet what to do with it.
Hope that helps,
Andy