svata said:
I don't ask anyone her to help me to cheat.
That's good to know;-) And my question wasn't meant as an insult but
just to remind you that asking her for help if you're not allowed to
is a stupid idea (one reason of many being that your lecturere could
very well be reading this group;-)
<program code snipped>
Sorry, but as others already have told you, your program doesn't even
compile. In main() neither 'choice' nor 'sub_sum' are defined. It's
simple to add 'choice' but the value of 'sub_sum' gets used but never
set - obviously, you would you like that to be the value of the variable
with the same name defined in store_data(), but that's only visible
in that function. not in main().
And your program is messing around with memory in bad ways. To start
with, you allocate memory for one menu item less than you have (FIRST
is 7, but you have 8 menu items) and treat the memory you got as if
there would be enough. Then, in the infinite loop in main(), you
allocate memory for the menu strings each time round. This is, of
course, useless and you also lose the pointers to memory you already
got before, so you're unable to deallocate it - you're program is
"leaking" memory. Things only get worse in your function store_data(),
there are lots of off-by-one errors. No wonder you earlier or later
get a segmentation fault, it's all rather a mess.
I think that you should rewrite that program. And the rewrite should
include a complete redesign of the data structures. At the moment you
store "generic" goods like "Fruit" in the same kind of structure you
use for real goods, e.g. "Apples" or "Bananas". This doesn't make much
sense and makes it hard to understand your program (I am still not sure
what it's actually supposed to do...) as well as dealing with the
different kinds of things. Picking good data structures is essential
for writing good programs. Here's one way you could do it, using
different types of structures for the categories (basically equivalent
to your main menus entries) and the "real" goods the user buys:
typdef structure {
const char *name; /* name of "real" item bought */
double price; /* it's price */
} Goods_T;
typedef structure {
const char *name; /* name of category i.e. "Fruit" */
Goods_T *list; /* of bought goods belonging to this category */
int list_length; /* length of the array of bought goods */
double sub_total; /* total price of all goods in this category */
} Category_T;
You would start with allocating memory for as many category structures
as needed (8 at the moment). For each category you assign a name from the
p_menu_items array (no memory allocation needed - you already got memory
for these strings when you defined p_menu_items, so you just need to
assign a pointer to the relevant element of that array to the name member),
and you set 'list' to NULL, 'list_len' to 0 and 'total' to 0.0. Of course,
you can also start with simply defining an array of these structures and
initialize them in a single step, so you don't need malloc() at all for
the categories:
Category_T categories [ ] = { { "Bread", NULL, 0, 0.0 },
{ "Butter", NULL, 0, 0.0 },
... };
In that case you also don't need the p_mebu_items array anymore.
This out of the way you can ask the user what (s)he wants to buy. When
(s)he selects one of the menu items and then enters the name for some
"real" item you increase the length of the list of bought goods simply
by using realloc() on the 'list' member of the category structure the
item is supposed to go in - if you didn't know, you can use realloc()
also when you don't have any memory yet, if you call it with NULL as
the first argument it behaves like malloc(). Don't forget to increment
the list length variable. For the new list element you now allocate
memory for the name and the you set the price. You also add price to
the sub_total member of the category structure - that way you can keep
a sum for each of the categories, and when you have to print out the
final total you have to iterate over just the categories.
I guess that when you use data structures more suited to your problem
also writing the program will become simpler - you can have functions
that deal with categories and functions for individual items the user
buys and you don't have to do lots of checks what you're dealing with
at any moment. That, in turn, will make your program easier to under-
stand and thus to debug if there still are bugs.
With the new data structures spend some time writing functions. You
should have a function to add a new item to one of the categories, you
should have a function to print a category, using another function that
prints a single item etc. Have functions for the different types o
user input instead of mixing it all together. In the end, you will
have a very simple main() function that contains a loop and just a few
function calls. And each function will also be simple and easy to test.
Regards, Jens
PS:
// Neccessary to align, because there are numbers greater than 9
if ( i < 1 ) {
printf("\t\t\t %d. %s\n", ( i + 9 ), p_menu_choices
);
}
else {
printf("\t\t\t%d. %s\n", ( i + 9 ), p_menu_choices);
}
can be replaced by
printf("\t\t\t%2d. %s\n", ( i + 9 ), p_menu_choices);
"%2d" means: use at least two places to print that int, if necessary
putting a space char in front.