database problem

This is a discussion on database problem within the C++ Programming forums, part of the General Programming Boards category; i have a small problem , when i exit the program ,open it again and load ( nothing loaded !!!!) ...

database problem

i have a small problem , when i exit the program ,open it again and load ( nothing loaded !!!!) i select display .. nothin is displayed.. i don't know if the problem is in the save() function or in the load() function ....

the problem has to do with the way you are using i. Follow through with i for the first student info you add. In main() i is assigned zero. You then pass i to add() by reference. In add() i is incremented since zero is less than 200. i is now one. when add() terminates the value of i is still one back in main(). Then you send i by value to save() where you use it as the index of the student to save. However, there is no student at info[1], there is only a student at info[0] at this point. Therefore, you will either store junk in the file or nothing. Oooops. To store the last student added to info[] try using an index with value i - 1 like this:

I copied and pasted your program into DevC++. I made the change to the index in save() and load() and added some code in load() so I could see what was actually loaded back, and it worked fine for a single student. The problem came when I closed the program and restarted it. When I tried to read from file it didn't work because i was now 0 instead of 1, so when the correction was made as above, it caused a fence post error on the short side causing me to try to read into index -1 instead of 0.

What to do? Well, I'd try passing only an array to load() and have load() read whatever is in the file into my array, overwriting what was already there, if anything. I'd keep a counter in load() to make sure I didn't read more than the 200 possible student values.

I've played with the program for some time now. I couldn't get it to work with your syntax so I changed a few things like making the variable names clearer, dropping the ios::binary, and using member by member read/write of structs. I also wrote the number of structs to be written to the file and to be read from the file to the file to make the syntax a little cleaner to me. I changed the save() function to save the entire info array each time the function was called by using the default ios::trunc rather than writing a single student to file each time save() called using ios::app. With those changes I got the program to work fine. It appears below.

Then I tried using ios::binary rather than ios::trunc and that worked fine. Then I switched to full struct read/write rather than member by member read/write, and it flunked again. I tried changing the cast type to C style cast, but to no avail. However, I can't explain why this didn't work, as it should, from what little I know of that syntax.

You really should test the files to be sure they are open before use and using the keyword const in a number of places is probably also worthwhile, but those are minor points for the learning process, and not the major points of the project, I'm sure.