I'm working on an assignment from school and I am stumped. I'm not a C++ newb per say but I don't have alot of experience with dealing with read/write with files. Case 3 is my first issue, when I run the report it prints the employee info fine, but if I go to run it again it prints nothing.
Case 2 is also got me hitting a brick wall, but we can deal with one thing at a time.
I suspect the problem with case 3 not working more than once is that after running once, you've reached the end of the file, so when you try to go through again you're already at the end and finish immediately. You should try to find a way to start at the beginning again... (hint: you'll be manipulating the file pointer, so look for functions that do that)
A minor problem at line 18, while(loop = true) should use the == comparison operator, not the = assignment operator.
This is quite an interesting program. If you want the "report" option to reflect the changes made in the "enter employee" option, there are a few things you might need to do.
One is to close and re-open the two files each time around the loop. Another is to keep the output file open, but use the flush() function at the end of adding a new employee. The report file won't reflect the changes unless it's closed and opened again.
Another approach is to use a single file (which is what it is, in reality) for both input and output, and open it with the options ios_base::app | ios_base::in | ios_base::out or something like that. By the way, ios_base::app doesn't make much sense for an ifstream (input only).
You will as suggested have to reset the file position to read from the beginning when producing the report. The use of clear() may also be required after reading to the end of the file, or after a possible write error (maybe in the case of bad user input).
@Zhuge, now that you say it, that makes perfect sense!
@Chervil, I am completely new to fstream operations so I was kinda just throwing stuff out there.
I get mostly what you are saying and will see about updating them to use the correct format. Besides just searching google is there a good place for an in-depth review of fstream?
Thanks again all! I'll come back with the progress and probably have one or two more questions.
Basically, the capabilities give you a set of tools, the trick is to know which tool to use for which purpose. Largely, you can only learn properly by actually using them.
If you can run your program with a debugger, I'd recommend monitoring the state of the .good() flag. When things don't work as you expect, it's often because some previous action has set one of the condition flags.
If you don't have a debugger, it may be worth adding temporary debugging messages at key points in your program (just before and/or just after using the stream), to display the state of the stream: good() and possibly also bad(), fail() and eof().
Quick update, case 3 works perfectly! I also have a much better understanding of fstream (though I still need more time playing around with it to fully get it all).
I'm going to attempt case 2, though it's search function through the string confuses me a bit.
Line 12 of your case 2:, what is (int)line.length() supposed to do? I've never seen a function call like that. I understand .length is a member function of the line class, but (int) at the beginning? Not sure what that's supposed to do, but I'm no expert by any means, especially with classes and such, so if you know it to be correct please disregard. :)
If you are simply calling the length member of the string class, which it looks like, it'll return the length as an int anyway (or at least, an unsigned integral type according to the reference for it) so you could just use for (int x=0; x <= line.length(); x++)
@Chervil. "Debug Assertion Failed!" It doesn't show up as an error in the error list. So I guess a program crash. I did some testing and it seems the problem is how x is compared to the length of the string.
@Raezzor. I am attempting to cast line.length to an int (I don't know how casting works in C++, but that is how it works in C#). I don't know what the problem is but I figured it might be due to it being an unsigned int.
No problem. Like I said I'm no expert either, so you may be right. But I've used .length myself in test expressions and never had to cast it, but then I may have just gotten lucky. :)
From what I've read on the net, it maybe something to do with your file opening being NULL. Add in a code to check if the file is open or not before going through ur code.
Also, your case 2 code seems really weird...
There's nothing declared for search[] and result.
Your switch code is missing breaks();.
Your setting tempWord = ""; before going into the switch, and then, tempFirstName = tempWord;
I'm guessing you've declared the missing search[] and result some where else?