T
Tobias Oed
David said:Could someone help me out here?
I am trying to get my program to delete arrays within
a structure named item. It is for an assignment at school
and I just can't figure this one out. For some reason, when
I run through to the print option, I notice that the program
is not recognizing my attempt to errase in the delete function.
Thanks for your help.
#include <stdio.h>
#define n 25
#define m 10
By convention macro names are UPPER case. Also, their name should reflect
their usage.
#define M MAX_ITEMS
#define N MAX_NAME_LEN
struct item{
int id;
char name[n];
float cost;
char category;
} items[m];
int x=0;
This x counts the number of item in items. Globals are pretty bad but
globals with poor names are awfull.
int n_items=0;
int find(int id){
int j;
for(j=0; j<x; j++){
if(items[j].id==id)
return j;
}
return -1;
}
void add(void){
int id;
if(x==m){ //Do not change.
printf("Sorry too many items in database!\n");
return;
}
printf("Enter item id: ");
scanf("%d", &id);
items[x].id=id;
if(find(id) >=0){ //Do not change
printf("Item prexists entry attempt.\n");
return;
}
printf("Please enter the product's name: ");
scanf("%s", items[x].name);
scanf is difficult to use. In this case if the user enters more than n-1
characters, you start to trample all over memory. Prefer fgets followed by
sscanf (if you need to convert)
printf("What is the price of the item: ");
scanf("%f", &items[x].cost);
printf("Please enter the category for the item:\n\t M=meat\n\t
P=produce\n\t D=dairy\n\t C=canned foods\n\t N=nonfoods:
\n");
scanf("%s", &items[x].category);
x++;
return;
}
void deletes(void){
int id, j;
printf("\nEnter item id: ");
scanf("%d", &id);
if(find(id) >=0){
for(j=0; j<x; j++){
find() returns the index, so why search again? Just save the index (in the
same way you do in search() bellow).
if(items[j].id==id){
items[j].id=0;
items[j].name[n]=0;
items[j].cost=0;
items[j].category=0;
}
So far your code assumed that items 0...x-1 existed in the data base. This
code does something entierly different. What you want to do here is move
the last item (item[x-1]) into the spot of the item you want to delete
(item[j])
^^}
}
else
printf("Part Not Found in This Database!\n");
return;
}
void edit(void){
int id;
printf("Please enter the item number.");
scanf("%d", &id);
if(find(id) >=0){
printf("Please enter the new cost of the item: ");
scanf("%f", &items[20].cost);
where does that magic 20 come from? again, save the index returned by find,
and use that instead.
}
else
printf("Item not found!\n");
return;
}
void search(void){
int i, id;
printf("Please enter the item number: ");
scanf("%d", &id);
i=find(id);
if(i>=0){
printf("\nItem id is: %d\n", items.id);
printf("Item name is: %s\n", items.name);
printf("Item cost is: %.2f\n", items.cost);
printf("Item category is: %s\n", &items.category);
}
else
printf("Item was not found!\n");
}
void print(void){
int i;
printf("Item Id\t\tItem Name\tItem Cost\t\tItem Category\n");
printf("_______________________________________________________________
______________\n");
for(i=0; i<m; i++)
You only have x-1 items in your data base. Why print the nonexistant ones?
printf("%7d\t\t%s\t\t%.2f\t\t\t%s\n\n", items.id, items.name,
items.cost, &items.category);
printf has nicer ways than \t to get good looking formatted output
}
int main(void){
int choice;
printf("\n\n\tTHE GROCERY STORE DATABASE!\n\n");
for( ; ; ){
printf("\t1.\tADD NEW ITEM.\n\t2.\tDELETE ITEM.\n\t3.\tEDIT
ITEM(CHANGE COST).\n\t4.\tSEARCH FOR
ITEM.\n\t5.\tDISPLAY INVENTORY DETAILS\n\t6.\tQUIT\n");
You can glue string literals toghether over multiple lines:
"hello, "
"world"
Is the same as "hello, world"
printf("\t");
scanf("%d", &choice);
while(getchar()!= '\n');
put the ; on a line of its own to emphasize the empty body loop.
Told you scanf was hard, that's why you need the loop in the first place.
switch(choice){
case 1: add();
break;
case 2: deletes();
break;
case 3: edit();
break;
case 4: search();
break;
case 5: print();
break;
case 6:
return 0;
default:
printf("Try Again!\n");
}//end switch
}//endfor
return 0;
}//end main
Tobias.