Here's a corker for you guys

This is a discussion on Here's a corker for you guys within the C++ Programming forums, part of the General Programming Boards category; Ok, I've got a relatively large program here.
The program is that it temporarily populates the main array with an ...

Here's a corker for you guys

Ok, I've got a relatively large program here.

The program is that it temporarily populates the main array with an additional element (somehow taken from the last entered element). This temporary element contains one of the variables inputed by the user, but the rest remains blank. It is not saved to the disk. I'm not sure why this is happening, but I wan't to stop it. If anyone can see what's causing it, I'm all ears.

You count variable is being incremented an extra time because of the way you are reading the file which is messing up your order. Decrement the count variable by one before your first call to mainmenu() in main and it should work.

If you have a reasonably large piece of code and you're unsure where your error is, it can help to run it through your compilers debugger (if it's got one) and just watch some of the variables.

The bigger problem is how you are using the file. When you write to the file you always have a # sign at the end of every item. BUT you don't want a # at the end of the last file, you want the EOF marker right after the last item. As it is when you read in the item members using getline() with # as the delimiter you do fine until you get to the end of the last item. Then getline() finds the last #, but it still hasn't found EOF yet. It has read in all of the last item, but it hasn't encountered EOF so the loop condition is still true and the entire loop runs again, but what does it read in??? Probably whatever was in the appropriate input buffer from the previous read in, thus causing the duplicate/extra entry at the end.

How to prevent this??? Avoid the # at the end of the last item.

How? By writing each item to file differently. First initialize count to -1. Then increment count before each item is written to (or read from) file. If count is 0 write the first line of data[0] without a preceding # sign and write the # sign before any other data. Otherwise write the # and the new line to the end of the last line of the preceding data[i] before writing the next data[i].

I have to go now. If you want to see code on how to do this post and I will write it if no one else has already.