Code:
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define SZ 100
typedef struct
{
char owner[SZ];
char type[SZ];
char name[SZ];
char date[SZ];
char sex;
int age;
double cost;
}pet;
void GetPet(pet mypet[]);
void Menu();
void addEntry(pet mypet[], int numentries);
int GetIndex(pet mypet[]);
void deleteEntry(pet mypet[], int numentries, int index);
void setAge(pet mypet[], int numentries);
void Display(pet mypet[]);
void Save(pet mypet[]);
void Clear(pet mypet[]);
char Quit();
int main()
{
pet mypet[SZ];
int numentries=5;
char choice;
char again='Y';
int index;
while(again=='Y' || 'y')
{
GetPet(mypet);
setAge(mypet, numentries);
Menu();
scanf(" %c", &choice);
switch(choice)
{
case 'A': addEntry(mypet, numentries);
numentries++;
break;
case 'D': index=GetIndex(mypet);
deleteEntry(mypet, numentries, index);
numentries--;
break;
case 'P': Display(mypet);
break;
case 'S': Save(mypet);
break;
case 'C': Clear(mypet);
numentries=0;
break;
case 'Q': again=Quit();
break;
}
}
return 0;
}
void GetPet(pet mypet[])
{
strcpy(mypet[0].owner,"Doe");
strcpy(mypet[0].type,"Snake");
strcpy(mypet[0].name,"Sassy");
strcpy(mypet[0].date,"11/23/07");
mypet[0].sex='F';
mypet[0].age=9;
mypet[0].cost=112.56;
strcpy(mypet[1].owner,"Johnson");
strcpy(mypet[1].type,"Parrot");
strcpy(mypet[1].name,"Polly");
strcpy(mypet[1].date,"1/14/06");
mypet[1].sex='M';
mypet[1].age=15;
mypet[1].cost=224.90;
strcpy(mypet[2].owner,"Willruth");
strcpy(mypet[2].type,"Dog");
strcpy(mypet[2].name,"Winston");
strcpy(mypet[2].date,"5/28/07");
mypet[2].sex='M';
mypet[2].age=10;
mypet[2].cost=157.32;
strcpy(mypet[3].owner,"Mendes");
strcpy(mypet[3].type,"Cat");
strcpy(mypet[3].name,"Dandylion");
strcpy(mypet[3].date,"2/07/07");
mypet[3].sex='F';
mypet[3].age=2;
mypet[3].cost=300.87;
strcpy(mypet[4].owner,"Basic");
strcpy(mypet[4].type,"Cat");
strcpy(mypet[4].name,"Kiki");
strcpy(mypet[4].date,"6/17/05");
mypet[4].sex='F';
mypet[4].age=3;
mypet[4].cost=273.99;
}
void Menu()
{
printf("\nA....Add a new record");
printf("\nD....Delete a record");
printf("\nP....Display all records");
printf("\nS....Save all records to a file");
printf("\nC....Clear all records");
printf("\nQ....Quit");
}
void addEntry(pet mypet[], int numentries)
{
printf("\nEnter owner's name: ");
scanf(" %s", mypet[numentries].owner);
printf("\nEnter type of pet: ");
scanf(" %s", mypet[numentries].type);
printf("\nEnter name of pet: ");
scanf(" %s", mypet[numentries].name);
printf("\nEnter date the pet was last seen: ");
scanf(" %s", mypet[numentries].date);
printf("\nEnter sex of the pet: ");
scanf(" %c", mypet[numentries].sex);
printf("\nEnter age of the pet: ");
scanf(" %d", mypet[numentries].age);
printf("\nEnter the cost of the visit: ");
scanf(" %lf", mypet[numentries].cost);
}
int GetIndex(pet mypet[])
{
char lastName[SZ];
char petName[SZ];
int i;
printf("\nEnter last name of the owner: ");
scanf(" %s", lastName);
printf("\nEnter name of pet: ");
scanf(" %s", petName);
for(i=0;i<SZ;i++)
{
if(strcmp(lastName, mypet[i].owner)==0 && strcmp(petName, mypet[i].name)==0)
{
return i;
}
else return -1;
}
}
void deleteEntry(pet mypet[], int numentries, int index)
{
mypet[index]=mypet[numentries-1];
}
void setAge(pet mypet[], int numentries)
{
int j;
for(j=numentries; j<SZ; j++)
{
mypet[j].age=-1;
}
}
void Diplay(pet mypet[])
{
int k;
for(k=0;k<SZ;k++)
{
if(mypet[k].age!=-1)
{
printf("\nOwner: %s", mypet[k].owner);
printf("\nType of animal: %s", mypet[k].type);
printf("\nName of pet: %s", mypet[k].name);
printf("\nDate animal was last seen: %s", mypet[k].date);
printf("\nSex of animal: %c", mypet[k].sex);
printf("\nAge of animal: %d", mypet[k].age);
printf("\nCost of visit: %f", mypet[k].cost);
}
}
}
void Save(pet mypet[])
{
int x;
FILE *outp;
outp=fopen("vet.txt", "w");
for(x=0;x<SZ;x++)
{
if(mypet[x].age!=-1)
{
fprintf(outp, "\nThe owners name is %s", mypet[x].owner);
fprintf(outp, "\nThe type of pet is %s", mypet[x].type);
fprintf(outp, "\nThe name of the pet is %s", mypet[x].name);
fprintf(outp, "\nThe date pet was last seen is %s", mypet[x].date);
fprintf(outp, "\nThe sex of the pet is %c", mypet[x].sex);
fprintf(outp, "\nThe age of the animal is %d", mypet[x].age);
fprintf(outp, "\nThe cost of the visit is %f", mypet[x].cost);
}
}
fclose(outp);
}
void Clear(pet mypet[])
{
int y;
for(y=0;y<SZ;y++)
{
mypet[y].age=-1;
}
}
char Quit()
{
char input='N';
return input;
}