this is the program instructions. I am so close but I do not know what is wrong.
ID: 3813999 • Letter: T
Question
this is the program instructions. I am so close but I do not know what is wrong. Can someone help me please? I will make sure to thumbs up your answer. Thank you SO SO much
#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);
//uses getchar to remove the character
getchar();
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);
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;
}
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;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define NAME_LEN thirty
struct dog;
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 Associate in Nursing computer code, *
* then calls a operate to perform the requested *
* action. Repeats till the user enters the *
* command 'q'. Prints a slip message if the user *
* enters Associate in Nursing smuggled code. *
**********************************************************/
int main(void)
information and scans it
printf("Enter number:");
scanf("%d", &num);
//uses getchar to get rid of the character
getchar();
printf("enter dogs name");
fgets(dogName,NAME_LEN+1, stdin);
printf("whats the dogs breed");
fgets(breed1,NAME_LEN+1, stdin);
printf("whats the house owners last name");
fgets(ownerLastName,NAME_LEN+1, stdin);
struct dog *temp = (struct dog*)malloc(sizeof(struct dog));
//if there's nothing within list, puts num within, and copies
//dogname, breed so on, and puts next temporary worker price to be null.
if (list ==NULL)come temp;
}
//if list has numbers within then perform this
else
victimisation temporary worker.
temporary worker = list;
whereas (temp!= NULL){
if (temp ->number == num)
bool =1;
temporary worker = temp->next;
}
if (bool==1)therefore i will get from user with additional area for null temrinating
char dogName [NAME_LEN+1];
printf("Enter dog’s name please");
//stores it in
fgets(dogName, NAME_LEN+1, stdin);
//temp so as to check and search.
struct dog *temp= list;
whereas (temp != NULL)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.