Input/Output and Files

D

David

Could someone help me turn this program into a program that accepts
files(database) as an input and writes to a file as method of saving
the database information?

#include <stdio.h>

#define n 25
#define m 10

struct item{
int id;
char name[n];
float cost;
char category;
} items[m];
int x=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){
printf("Sorry too many items in database!\n");
return;
}

printf("Enter item id: ");
scanf("%d", &id);
items[x].id=id;
if(find(id) >=0){
printf("Item prexists entry attempt.\n");
return;
}

printf("Please enter the product's name: ");
scanf("%s", items[x].name);
printf("What is the price of the item: $");
scanf("%f", &items[x].cost);
printf ("Please enter the category for the item:\n"
"\tM=MEAT\n"
"\tP=PRODUCE\n"
"\tD=DAIRY\n"
"\tC=CANNED FOODS\n"
"\tN=NONFOODS\n");
scanf("%s", &items[x].category);
x++;
return;
}

void deletes(void){
int id, j, k;
printf("\nEnter item id: ");
scanf("%d", &id);
j=find(id);
if(j>=0){
for(k=j;k<x;++k)
items[k]=items[k+1];
}
else{
printf("Part Not Found in This Database!\n");
}
x--;
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);
}
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++)
printf("%7d\t\t%s\t\t%.2f\t\t\t%s\n\n", items.id, items.name,
items.cost, &items.category);
}

int main(void){
int choice;
printf("\n\n\tTHE GROCERY STORE DATABASE!\n\n");
for( ; ; ){
printf("Enter the number of your choice:\n"
"\t1.\tADD ITEM.\n"
"\t2.\tDELETE ITEM.\n"
"\t3.\tEDIT ITEM (CHANGE COST).\n"
"\t4.\tSEARCH FOR ITEM.\n"
"\t5.\tDISPLAY DATABASE CONTENTS.\n"
"\t6.\tQUIT.\n");
printf("\t");
scanf("%d", &choice);
while(getchar()!= '\n');
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top