#include #include #include struct vaccine char name[20]; char code[5]; char country[10]; int dosage; int population; }; void displayvacc() { int i; int count = 0; count = readFile(); if (count < 0) puts("cannot open file"); printf(" \t\t\t INVENTORY \n"); printf(" NAMEVACCINE CODE|PRODUCING COUNTRY|DOSAGE REQUIRED|POPULATION COVERED \n"); for (i=0;i
could you pelase fix the code ?
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
struct vaccine
char name[20];
char code[5];
char country[10];
int dosage;
int population;
};
void displayvacc()
{
int i;
int count = 0;
count = readFile();
if (count < 0)
puts("cannot open file");
printf(" \t\t\t INVENTORY \n");
printf(" NAMEVACCINE CODE|PRODUCING COUNTRY|DOSAGE REQUIRED|POPULATION COVERED \n");
for (i=0;i<count;i++)
{
printf("%s %-10s %-8s %-6d %-4d \n", i+1, vacc[i].name,vacc[i].code,vacc[i].country,vacc[i].dosage,vacc[i].population);
}
}
void addvacc()
{
printf("Enter New Vaccine\n");
readFile();
if (count>0)
{
CODEChecker(0,count);
}
else
{
printf("\nVaccine Name: ");
fflush(stdin);
gets(vacc[count].name);
}
printf("Vaccine Code: ");
gets(vacc[count].code);
printf("Producing Country: ");
gets(vacc[count].country);
printf("Dosage Required: ");scanf("%d",&vacc[count].dosage);
printf("Percentage Population Covered : ");scanf("%d",&vacc[count].dosage);
writefile();
}
void deletevacc()
{
count=readFile();
char code[5];
int i,j;
int z=false;
printf("Enter Code of Vaccine to delete: ");
fflush(stdin);
gets(code);
for(i=0;i<count;i++)
{
z=true;
if(strcmp(vacc[i].code,code)==0)
{
for( j=i; j<(count-1); j++)
{
vacc[j]=vacc[j+1];
}
count--;
}
}
if(z==false)
{
}
writefile();
}
void editvacc()
{
char code[5];
int test;
int i;
int choice;
printf("Edit Vaccine!");
printf("\nEnter the code of vaccine to edit: ");
fflush(stdin);
gets(code);
test=checkCODE(code);
if (test == 0)
{
printf("The code %s is not found.", code);
else
{
readFile();
{
for(i=0;i<count;i++)
{
if(strcmp(id,vacc[i].code)!=0)
writefile();
else
{
printf("\n 1. Update Name of Vaccine?");
printf("\n 2. Update Vaccine Code?");
printf("\n 3. Update Producing Country?");
printf("\n 4. Update Dosage Required?");
printf("\n 5. Update Population Covered?");
printf("\n Enter choice:");
fflush(stdin);
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter new Name: ");
fflush(stdin);
gets(vacc[i].name);
break;
case 2:
printf("Enter new Code: ");
fflush(stdin);
gets(vacc[i].code);
break;
case 3:
printf("Enter new Country: ");
fflush(stdin);
gets(vacc[i].country);
break;
case 4:
printf("Enter new Dosage: ");
scanf("%d",&vacc[i].dosage);
break;
case 5:
printf("Enter new Population: ");
scanf("%d",&vacc[i].population);
default:
printf("Wrong Choice");
break;
}
writefile();
}
}
}
fclose(f);
f = fopen("Vaccine inventory.txt", "r");
readFile();
{
writefile();
}
fclose(f);
Printf("Inventory Updated");
}
}
int checkCODE(char code[])
{
int i;
count=readFile();
readFile();
for(i=0;i<count;i++)
{
if(strcmp(code,vacc[i].code)!=0)
{
fclose(f);
}
return 1;
}
fclose(f);
return 0;
}
int main ()
{
int choice;
count = readFile();
if(count < 0)
printf("Cannot locate file\n");
do
{
printf("\n");
printf("\t\t\t VACCINE INVENTORY MANAGEMENT SYSTEM \n");
printf("\t\t\t ........................................");
printf("\nPress:");
printf("\n 1.) Display all vacinnes.");
printf("\n 2.) Add new Vacinne.");
printf("\n 3.) Edit Vacinne.");
printf("\n 4.) Delete a Vaccine");
printf("\n 5.) Exit the Inventory.");
printf("\n Enter Choice__: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
displayvacc();
break;
case 2:
addvacc();
break;
case 3:
editvacc();
break;
case 4:
deletevacc();
break;
case 5:
exit(1);
break;
default :
printf("Wrong choice, please try another option!!");
}
}
while(choice!=5);
printf("Thank you");
}
struct vaccine vacc[30];
int count = 0;
FILE *f;
int writefile()
{
int i;
f = fopen("Vaccine inventory.txt", "w");
if (f == NULL)
return -1;
fprintf(f, "%d\n", count);
for (i = 0; i < count; ++i)
{
fputs(vacc[i].name, f);
fprintf(f, "\n");
fputs(vacc[i].code, f);
fprintf(f, "\n");
fputs(vacc[i].country, f);
fprintf(f, "\n");
fprintf(f, "%d\n", vacc[i].dosage);
fprintf(f, "%d\n", vacc[i].population);
}
fclose(f);
return 0;
}
int readFile()
{
int n = 0;
int i;
f = fopen("Vaccine inventory.txt", "r");
if (f == NULL)
return -1;
fscanf(f, "%d\n", &n);
for (i = 0; i < n; ++i)
{
fgets(vacc[i].name, 30, f);
vacc[i].name[strlen(vacc[i].name)-1] = 0;
fgets(vacc[i].code, 5, f);
vacc[i].code[strlen(vacc[i].code)-1] = 0;
fgets(vacc[i].country, 10, f);
vacc[i].country[strlen(vacc[i].country)-1] = 0;
fscanf(f, "%d", &vacc[i].dosage);
fscanf(f, "%d", &vacc[i].population);
}
fclose(f);
return n;
}
Step by step
Solved in 2 steps