I am modifying a previous assignment program to add 2 more menu items. 1 additional menu option to write structure data input by user into a file for insured items, and for uninsured items into a separate file; namely insured.dat and uninsured.dat.
The problem I am having is with what is being written to the files.
When program runs, and (my option 5) menu option selected, my insured.dat file will store:
abc I assume one for each items insured.
abc
also uninsured.dat will store only one(because only one uninsured item input) but it is still the first entry
abc
I don't know if it is a problem with my loop, but I have tried while loops, do while loops. I tried reinterpret_cast operator, with out the reinterpret.... I don't know what the whole structure will not store in the file.
P.S. I know I could clean this code up with some extra time. i.e. switch/case etc.
I see a couple of problems in your write() function.
First and probably most important, the sizeof() calls are probably not producing the value you expect.
Second how are you "viewing" the data that your file contains? Remember a binary file is not considered human readable, you'll need a "hex-dump" type of program to view the file contents. What's with the cout calls? And what happens if the file opening fails? You probably should write your "read" function to be able to verify the values were written correctly.
Third you don't actually need the else if(), a simple else should be all you need and you should stick with the C++ style casts instead of the C style casts.
Lastly since your file close() calls are at the end of the function, they are not really necessary. Just let the destructor close the files when the function returns.