Click here to Skip to main content
16,021,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#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("..............................................................................................\n");
        printf("    NAME     |  VACCINE CODE  |  PRODUCING COUNTRY  |  DOSAGE REQUIRED |  POPULATION COVERED  \n");
        printf("______________________________________________________________________________________________\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) 
        {
            count=readFile();
            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)
            {
                printf("Can't find Vaccine Code: %s .",code);
            }
        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;
    }


What I have tried:

I'm especially lost on how to declare the readFile
Posted
Updated 25-Aug-22 21:46pm

In C++, all functions must be declared before they are used, either by including the body of the whole function before any calls to it, or by using a forward reference.

Since you declare readFile at the end of your code, none of the functions above the definition have any idea what it's signature is, so they give an error when you try to call it.

Either move the definition to before the first function that calls it (displayvacc) or better add a forward reference to all your functions near the top of the file: 2.7 �� Forward declarations and definitions – Learn C++[^]
 
Share this answer
 
Comments
CPallini 26-Aug-22 2:03am    
5.
[no name] 26-Aug-22 4:07am    
I absolutely never use forward declarations. One of the most stupid ideas in the development of C/C++.
you have another sever flaw in your code at
C++
if (count < 0)
        puts("cannot open file");
You better but this in braces and use the else statement to impove your code. Like

C++
if (count < 0) {
        puts("cannot open file");
} else {
// only here is your reading and output useful
}
Consider using some class design for your struct vaccine and use input parameters for your functions.

There is a lot of room for improvement after fixing the biggest problems. ;-)
 
Share this answer
 
The source code seems to be C and not C++ except for true and false.

But the two boolean expressions are also used rather like in C, because something like this
should not happen in C++:
C
int z = false;

The compiler does not find several declarations:
vacc.c(30): warning C4013: "readFile" undefined; assumption: external with return type int
vacc.c(39): error C2065: "vacc": undeclared identifier
vacc.c(39): error C2109: index requires an array or pointer type
vacc.c(39): warning C4477: "printf": The format string "%s" requires an argument of type "char *", but the variadic argument "1" has type "int".
vacc.c(39): warning C4473: "printf": not enough arguments passed for format string
vacc.c(39): note: placeholders and their parameters expect 5 variadic arguments, but 1 variadic argument was provided.
vacc.c(39): note: The missing variadic argument "2" is required for the string "%-10s".
vacc.c(47): error C2065: "count": undeclared identifier


This can be easily solved by moving it to the beginning of the source code under the declaration of the struct vaccine.

C
struct vaccine vacc[30];
int count = 0;
FILE *f;

int writefile();
int readFile();

// does it exist?
void CODEChecker(int x, int count) {};

But there are still some questions that the author should think about.

Should it be C or C++?
Declaration or use of true and false

vacc.c(66): warning C4013: "gets" undefined; assumption: external with return type int
vacc.c(82): error C2065: "false": undeclared identifier
vacc.c(89): error C2065: "true": undeclared identifier
vacc.c(99): error C2065: "false": undeclared identifier
vacc.c(116): warning C4013: "checkCODE" undefined; assumption: external with return type int
vacc.c(127): error C2065: "id": undeclared identifier
vacc.c(127): warning C4047: "function": number of dereferences for "const char *" and "int" different
vacc.c(127): warning C4024: "strcmp": different types for formal and passed parameter 1


*the compiler messages were processed with a translator and are not original.

//edit1:
The gets() function has been completely removed from C since version C11 due to problems. Instead, a safer variant called gets_s() should be used, which uses the maximum number of characters to be read as an additional parameter:
char *gets_s( char *str, rsize_t n );


// edit2:

count = readFile();
gets_s(vacc[count].code, sizeof(vacc[0].code));

To prevent count from becoming negative, you could check at the beginning if the file exists and create it without data if necessary.

If the file does not exist count has the value -1. Accessing the array vacc with -1 leads to a crash. It would be better to keep the name of the file in a variable:

char* const vacc_filename = "Vaccine inventory.txt";


C
int main()
{
  if (!file_exists(vacc_filename)) {
     count = 0;
     writefile(vacc_filename);
  }

  count = readFile(vacc_filename);
  ...
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900