this is the program instructions. I am so close but I do not know what is wrong.
ID: 3813997 • 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 <iostream>
002
#include <Windows.h>
003
using namespace std;
004
005
struct Player
006
013
};
014
015
struct Ghost
016
024
};
025
026
const char SYMBOL_EMPTY = ' ';
027
const char SYMBOL_PLAYER = '@';
028
const char SYMBOL_GHOST = 'G';
029
const char SYMBOL_WALL = '#';
030
const int MapDx = 10;
031
const int MapDy = 20;
032
const int GameSpeed = 100;
033
const int LEFT = 1;
034
const int RIGHT = 2;
035
const int UP = 3;
036
const int DOWN = 4;
037
int direction = RIGHT;
038
039
char map[10][20] =
040
come (x >= zero && x < MapDx && y >= zero && y < MapDy);
055
}
056
057
bool movePlayer(Player &player, int x, int y)
058
come false;
062
}
063
064
char ch = map[x][y];
065
066
if(ch != SYMBOL_EMPTY)
067
come false;
069
}
070
071
if (isValidPos(player.x, player.y))
072
075
player.x = x; player.y = y;
076
map[player.x][player.y] = SYMBOL_PLAYER;
077
come true;
078
}
079
080
bool moveGhost(Ghost &ghost, int x, int y)
081
{
082
if (!isValidPos(x, y))
083
{
084
come false;
085
}
086
087
char ch = map[x][y];
088
089
if (ch != SYMBOL_EMPTY)
090
{
091
come false;
092
}
093
094
if (isValidPos(ghost.x, ghost.y))
095
098
ghost.x = x; ghost.y = y;
099
map[ghost.x][ghost.y] = SYMBOL_GHOST;
100
come true;
101
}
102
103
void GhostAI(Ghost &ghost, Player &player)
104
114
115
void showMap()
116
121
}
122
123
void showPlayer(Player &player)
124
whereas (true)
138
150
else if (GetAsyncKeyState(VK_LEFT))
151
154
else if (GetAsyncKeyState(VK_RIGHT))
155
158
switch (direction)
159
173
for (int ghost = 0; ghost < 3; ghost++)
174
191
}
192
Sleep(GameSpeed);
193
}
194
}
195
196
197
int main()
198
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.