Hello, I am currently working on a lot of different projects at my University, i
ID: 3672358 • Letter: H
Question
Hello, I am currently working on a lot of different projects at my University, i simply cannot give each project the time and care they need to get good scores. Please help me with the exercise below!
This is my assignment:
I need to create a database of dogs in a file, using the standard library fopen(). I need to include the name, breed and color of each dog!
The database file (database.dog) will be an array of records:
Example:
#define SZ_NAME 32
#define SZ_BREED 32
#define SZ_COLOR 16
struct dog_entry {
char name [SZ_NAME];
char breed [SZ_BREED];
char color [SZ_COLOR];
unsigned short weight;
unsigned char age;
char sex;
};
#define REC_SIZE sizeof(struct dog_entry)
Present the user with the following menu:
Enter 1 to add,
2 to change,
3 to delete,
4 to view,
5 to search,
6 to exit
Enter:
Explanation / Answer
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define SZ_NAME 32
#define SZ_BREED 32
#define SZ_COLOR 16
struct dog_entry {
char name [SZ_NAME];
char breed [SZ_BREED];
char color [SZ_COLOR];
};
#define REC_SIZE sizeof(struct dog_entry)
int main()
{
char ch, dog[SZ_NAME];
int i=0,j,n,c=1,flag;
struct dog_entry record[REC_SIZE];
do
{
printf("enter choice: 1.add 2.change 3.delete 4.view 5.search 6.exit ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter name,breed,color");
scanf("%s%s%s",record[i].name,record[i].breed,record[i].color);
i++;
n=i;
break;
case 2: printf("enter record index to change");
scanf("%d",&j);
printf("enter new values name,breed,color");
scanf("%s%s%s", record[j].name,record[j].breed,record[j].color);
break;
case 3: printf("enter index of record to delete ");
scanf("%d",&j);
record[j].name[SZ_NAME] = NULL;
record[j].breed[SZ_BREED] = NULL;
record[j].color[SZ_COLOR] = NULL;
break;
case 4: for(j=0;j<n;j++)
{
printf("%s %s %s", record[j].name, record[j].breed, record[j].color);
printf(" ");
}
break;
case 5: printf("enter name to search ");
scanf("%s",dog);
for(j=0;j<n;j++)
{
if(strcmp(record[j].name,dog)==0)
flag=1;
}
if(flag==1)
printf("record exists ");
else
printf("no such record exists ");
break;
default: break;
}
printf("enter 1 to use dog database, 0 to exit");
scanf("%d",&c);
}while(c==1);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.