I have tried a lot...but this is not working.

This is a discussion on I have tried a lot...but this is not working. within the C Programming forums, part of the General Programming Boards category; I have worked on this for a long time but just cant seem to figure it out...
This is a ...

>help????
I'm afraid to compile and run your code, but in such cases I suggest that you ditch what you have and start over, this time taking a more structured approach. In other words:

1) Create the inventory type.
2) Write a function to fill one variable of the inventory type from interactive input.
3) Write a function to write one variable of the inventory type to interactive output.
6) Write a function to update one variable of the inventory type.
4) Write a function to fill one variable of the inventory type from a file stream.
5) Write a function to write one variable of the inventory type to a file stream.

With these functions in hand you can do a surprising amount of useful things. More importantly, you are able to work with the smallest object in your program in its entirety. Just by using these functions in a loop, you have a database.

Your task after that is to work with sequences of inventory objects, such as sorting and searching. Both of these at their most naive are ridiculously easy, so you should have no problem there.

The key is to break the program down into teeny tiny pieces that you can work with and test individually so that when you put them together, they all work as they should. It saves you a lot of trouble when you have a few hundred (experienced programmers read this as hundred-thousand) lines of code and a bug that you can't seem to find.

Originally posted by Prelude
1) Create the inventory type.
2) Write a function to fill one variable of the inventory type from interactive input.
3) Write a function to write one variable of the inventory type to interactive output.
6) Write a function to update one variable of the inventory type.
4) Write a function to fill one variable of the inventory type from a file stream.
5) Write a function to write one variable of the inventory type to a file stream.

hmmm....okay....but can you explaing what you mean by^^^3

>2) Write a function to fill one variable of the inventory type from interactive input.

but then i have chars, ints and floats.....does that mean 3 diff functions.

>where was i going wrong...
The only obvious problem is that saveData doesn't close the file that it opened and you don't cose it elsewhere either. A cursory look doesn't show anything else seriously wrong as long as you avoid the undefined behavior that you know about already and assuming the pretty printing works as it should.

>I have simplified my programme to these 3 functions
Your problems are coming from addItems. And since you've apparently narrowed the problem down to these three functions, it should be easy to locate where things go wrong if you've been paying attention.

>int i=*arrayL;
Because you use feof as a loop condition when reading the file, *arrayL probably has a value one higher than you want, which is also probably why you subtract one from it when you save the data. As such, this should also be

int i = *arrayL - 1;

Or you could fix the code that you "already know" is wrong. When your willful ignorance effects the normal operation of your programs, it's time to take your head out of your ass and learn to do things the right way instead of try to (poorly) work around the issues that could easily be avoided.

>arrayL++;
This does nothing but advance the pointer to the next block of memory, it doesn't increment arrayL the way you think it does. Try this instead:

(*arrayL)++;

Note that while ++ and * have the same precedence, they are associated from right to left, so the ++ is performed first, resulting in moving the pointer to the next memory block and dereferencing that address. Parentheses force the dereference to be performed first, resulting in incrementing the contents of arrayL as you would expect.

>but you'd use the struct keyword for structures.
It is a typedef'd structure, so the code is correct.

>scrap the fflush(stdin) before the others catch it.
>lose gets(buf), and use fgets instead
nomi already asked that we not comment on these in another thread. We can inform the ignorant, but we can't help the stupid if they don't want us to.

Thx...prelude....but....i figured everything you pointed out before i even checked the post....

Although i dont know how gets is wrong???....

I figured that by using fgets i was putting in the '\n' character which was throwing all my formatting off course....plus that *array++....

>nomi already asked that we not comment on these in another thread. We can inform the ignorant, but we can't help the stupid if they don't want us to.

Is that a direct insult to me....coz if it is>...I have just started C and i currently have no plans to go professionally.....and i dont think i will ever...

>Then Why learn C?
Coz i had no idea about what programming was about and i always wanted to know some programming....So i have planned on learning a bit of Turing,C and Java...and besides i think you can make a lot of cool stuff with C...