Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have the project almost done. I have to: append: ask the user to enter patient

ID: 3812672 • Letter: I

Question

I have the project almost done. I have to:

append: ask the user to enter patient number, dog’s name, dog’s breed, and owner’s last name, then add the dog to the end of the linked list.

It should check whether the dog has already existed by patient number. If so, the function should print a message and exit.

If the dog does not exist, allocate memory for the dog, store the data, and append the dog to the end of the linked list.

If the list is empty, the function should return the pointer to the newly created dog.

Otherwise, add the dog to the end of the linked list and return the pointer to the linked

list.

search: find the dog by name, print all the dog’s information that matches the name. If the dog is not found, print a message.

print: print the name and number of all the dogs.

clear: when the user exists the program, all the memory allocated for the linked list should be

deallocated.

This is the code I have so far for this project.

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define NAME_LEN 30
struct dog{
   int number;
   char dog_name[NAME_LEN+1];
   char owner_last_name[NAME_LEN+1];
   char breed[NAME_LEN+1];
   struct dog *next;
};


struct dog *append(struct dog *list);
void search(struct dog *list);
void print(struct dog *list);
void clear(struct dog *list);
int read_line(char str[], int n);

/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/
int main(void)
{
char code;

struct dog *dog_list = NULL;
printf("Operation Code: a for appending to the list, s for finding a dog"
   ", p for printing the list; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': dog_list = append(dog_list);
break;
case 's': search(dog_list);
break;
case 'p': print(dog_list);
break;
case 'q': clear(dog_list);
       return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}

struct dog *append(struct dog *list){
   int num;
   char dogName[NAME_LEN+1];
   char ownerLastName[NAME_LEN+1];
   char breed1[NAME_LEN+1];
  
   //gets user info and scans it
   printf("Enter number:");
   scanf("%d", &num);

//should i use fflush(stdin); here? i dont know what it does.
   printf("enter dogs name");
   fgets(dogName,NAME_LEN+1, stdin);
   printf("whats the dogs breed");
   fgets(breed1,NAME_LEN+1, stdin);
   printf("whats the owners last name");
   fgets(ownerLastName,NAME_LEN+1, stdin);
  
   //WHAT DOES THIS CODE BELOW MEAN?
   struct dog *temp = (struct dog*)malloc(sizeof(struct dog));
  
   //if there is nothing inside list, puts num inside, and copies
   //dogname, breed and so on, and puts next temp value to be null.
   if (list ==NULL){
       temp->number=num;
       strcpy(temp->dog_name, dogName);
       strcpy(temp->breed, breed1);
       strcpy(temp->owner_last_name, ownerLastName);
       temp->next = NULL;
       return temp;
   }
   //if list has numbers inside then perform this
   else
   {
   int bool = 0;
  
   //checks if dog is already in system with using temp.
   temp = list;
   while (temp!= NULL){
       if (temp ->number == num)
           bool =1;
       temp = temp->next;
   }
   if (bool==1){
       printf("Dog is already in system!");
       return list;
   }
   //if dog is not in system, it creates a new one. also, what does below mean?
   // format of the dynamic allocation is new to me.
   struct dog * temp1 = (struct dog*)malloc(sizeof(struct dog));
   temp1->number = num;
   strcpy(temp1->dog_name, dogName);
   strcpy(temp1->breed, breed1);
   strcpy(temp1->owner_last_name, ownerLastName);
   temp1->next = NULL;
   temp = list;

   //while loop to go until end of list
   while (temp->next !=NULL){
       temp = temp->next;
   }
   temp->next = temp1;
   return list;
   }
}


void search (struct dog *list)
{
   //new char name dogName so i can get from user with extra room for null temrinating
   char dogName [NAME_LEN+1];
   printf("Enter dog’s name please");
   //stores it in
   fgets(dogName, NAME_LEN+1, stdin);
  
   //temp in order to compare and search.
   struct dog *temp= list;
   while (temp != NULL){
       if (!strcmp(temp->dog_name, dogName)){
           printf("number: %d Dog name: %s Breed: %s Owners name: %s ",
           temp->number, temp->dog_name, temp->breed, temp->owner_last_name);
       }
       //goes on to next dog number
       temp= temp->next;
   }
}
void print(struct dog *list){

   if (list==NULL){
       printf("There is no records of that");
   }
   struct dog * temp = list;
  
   //prints each record with while loop and ->next
   while (temp !=NULL){
       printf("number: %d Dog name: %s Breed: %s Owners name: %s ",
       temp->number, temp->dog_name, temp->breed, temp->owner_last_name);

       //goes on to next dog number
       temp= temp->next;
   }

}
void clear(struct dog *list)
{
   struct dog * temp = list;
   while (temp !=NULL)
   {
       temp= temp->next;
       free(temp);
   }

}

int read_line(char str[], int n)
{
int ch, i = 0;

while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
  
}
str[i] = '';
return i;
}

Please fix my current code to make it work! Thank you so much in advance. This will help me so much. I appreciate it.

Explanation / Answer

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define NAME_LEN 30
struct dog{
int number;
char dog_name[NAME_LEN+1];
char owner_last_name[NAME_LEN+1];
char breed[NAME_LEN+1];
struct dog *next;
};

struct dog *append(struct dog *list);
void search(struct dog *list);
void print(struct dog *list);
void clear(struct dog *list);
int read_line(char str[], int n);
/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/
int main(void)
{
char code;
struct dog *dog_list = NULL;
printf("Operation Code: a for appending to the list, s for finding a dog"
", p for printing the list; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': dog_list = append(dog_list);
break;
case 's': search(dog_list);
break;
case 'p': print(dog_list);
break;
case 'q': clear(dog_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
struct dog *append(struct dog *list){
int num;
char dogName[NAME_LEN+1];
char ownerLastName[NAME_LEN+1];
char breed1[NAME_LEN+1];
  
//gets user info and scans it
printf("Enter number:");
scanf("%d", &num);
getchar();
//Here we are using extra getchar(); to remove the from the input stream which is not removed by scanf.
   //should i use fflush(stdin); here? i dont know what it does.
printf("enter dogs name");
fgets(dogName,NAME_LEN+1, stdin);
printf("whats the dogs breed");
fgets(breed1,NAME_LEN+1, stdin);
printf("whats the owners last name");
fgets(ownerLastName,NAME_LEN+1, stdin);
  
//WHAT DOES THIS CODE BELOW MEAN?
struct dog *temp = (struct dog*)malloc(sizeof(struct dog));
  
//if there is nothing inside list, puts num inside, and copies
//dogname, breed and so on, and puts next temp value to be null.
if (list ==NULL){
temp->number=num;
strcpy(temp->dog_name, dogName);
strcpy(temp->breed, breed1);
strcpy(temp->owner_last_name, ownerLastName);
temp->next = NULL;
return temp;
}
//if list has numbers inside then perform this
else
{
int bool = 0;
  
//checks if dog is already in system with using temp.
temp = list;
while (temp!= NULL){
if (temp ->number == num)
bool =1;
temp = temp->next;
}
if (bool==1){
printf("Dog is already in system!");
return list;
}
//if dog is not in system, it creates a new one. also, what does below mean?
// format of the dynamic allocation is new to me.
struct dog * temp1 = (struct dog*)malloc(sizeof(struct dog));
temp1->number = num;
strcpy(temp1->dog_name, dogName);
strcpy(temp1->breed, breed1);
strcpy(temp1->owner_last_name, ownerLastName);
temp1->next = NULL;
temp = list;
//while loop to go until end of list
while (temp->next !=NULL){
temp = temp->next;
}
temp->next = temp1;
return list;
}
}

void search (struct dog *list)
{
//new char name dogName so i can get from user with extra room for null temrinating
char dogName [NAME_LEN+1];
printf("Enter dog’s name please");
//stores it in
fgets(dogName, NAME_LEN+1, stdin);
  
//temp in order to compare and search.
struct dog *temp= list;
while (temp != NULL){
if (!strcmp(temp->dog_name, dogName)){
printf("number: %d Dog name: %s Breed: %s Owners name: %s ",
temp->number, temp->dog_name, temp->breed, temp->owner_last_name);
}
//goes on to next dog number
temp= temp->next;
}
}
void print(struct dog *list){
if (list==NULL){
printf("There is no records of that");
}
struct dog * temp = list;
  
//prints each record with while loop and ->next
while (temp !=NULL){
printf("number: %d Dog name: %s Breed: %s Owners name: %s ",
temp->number, temp->dog_name, temp->breed, temp->owner_last_name);
//goes on to next dog number
temp= temp->next;
}
}
void clear(struct dog *list)
{
struct dog * temp = list;
while (temp !=NULL)
{
temp= temp->next;
free(temp);
}
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
  
}
str[i] = '';
return i;
}