http://pastebin.com/esxAR3wY
This is my source code here...It is simply too big and ugly to fit in here, so I used this site..
If anyone can paste the code into his compiler and help me I will be grateful.
Everything works except for the "addEntry" function (and I am guessing "resizeArrayCopy" function too)..so if you are willing to help me, look there.
My problem basically is:
When I set the buffer size to 4, then load a file that has 4 entries, then I try to add a new entry from the menu, entry number FIVE just shows up blank after that. Entries after this entry are perfectly fine tho.
When I load a file that has less than 4 elements, then fill up the buffer with my data, the FIFTH element shows some strange characters, and (I think, not 100% sure) that in this case, entries six and onward work perfectly. I have been trying to catch the problem this entire day, and couldn't. If you are willing to help me, you definately need to compile the code and run the program in order to see for yourselves what exactly is wrong, since the code is very ugly written and it is (quite) big..
PS: compiling with codeblocks

There are several problems with that program. There may be other problems that I have not yet found.

  1. addEntry() -- the first parameter needs to be passed by reference, not by value (See line below). That is because the array is reallocated at the start of the function, and that can not be seen by main() unless you pass a pointer to the pointer declared in main().
    void addEntry (Entry*& fpBuffer, int &elemCount,size_t &fBufferSize, size_t resizeStep);
  2. main() -- you need to initialize ALL the local variables because two integers are being used before they are initialized.
  3. Although not really a problem, since this is a c++ program the structure should contain std::string instead of char arrays. If you are not allowed to change that structure then you don't have any other choice, otherwise you should consider using string instead of char*.
  4. addEntry() -- the first if statement is incorrect. `if (elemCount==fBufferSize)' The value of elemSize is 0, the value of fpBuffer == NULL, and the value of fBufferSize is 4. You need to initialize fBufferSize to be 0 if you expect addEntry() to ever reallocate the pointer.
Entry* resizeArrayCopy(Entry*& fpBuffer, const size_t resizeStep, size_t &bufferSize)
{
    Entry* pNewBuffer = new Entry[bufferSize + resizeStep];

    if(pNewBuffer == NULL)
    {
        return NULL;
    }

In the above, new does not return NULL, instead it throws an exception, If you want to test for NULL you have to add a try/catch block. Here is how to do it

Entry* resizeArrayCopy(Entry*& fpBuffer, const size_t resizeStep, size_t &bufferSize)
{
    Entry* pNewBuffer;
    try
    {
        pNewBuffer = new Entry[bufferSize + resizeStep];
    }
    catch(bad_alloc xa) { 
    cout << "Allocation Failure\n"; 
    return NULL; 
   } 

Thanks for your help man, really! I only added the reference sign where you told me to, and it works fine now!
About point 2 - the first integer, elementsCounter gets initialized in the fileOpen function (because when the user loads a new file, the elements count must start from zero again), and the option variable gets inputted by the user, so still everything is fine for me!
Point 3. No problem there.
Point 4. The value of elementsCount gets changed by the openFile(count of elements read from file) addEntry (++), and by the deleteEntry(--) functions. It is equal to zero only if:
a) the user loaded a file with zero elements,or created a blank new one
b) when the user has deleted all the entries from the book.
But that reference sign there did the trick for me... what a stupid mistake really.. thanks a lot man!

Point 4. The value of elementsCount gets changed by the openFile

But not if you don't call openFile() first. For example I first added a new item then tried to save it to file. Doesn't work when called in that order.

BTW: You would have saved yourself an awful lot of debugging and grief had you used std::vector instead of dynamic arrays. std::vector does all the allocation for you.

Yes, I know, but STL library is out of the way for this one..It is all fine now, I added checks for "opened file" everywhere (this is the main idea of the program and it should be able to work only with a file loaded)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.