Question
Hi Can someone please help with my code errors. I keep getting errors because of the fgets and I don't know how to fix it.
Hi Can someone please help with my code errors. I keep getting errors because of the fgets and I don't know how to fix it. Language is C
Here is my code:
#include
#include
#include
#include
typedef struct failfish
{
int sequence_number;
struct failfish *next;
struct failfish *previous;
} failfish;
typedef struct failfish_queue
{
char *pondname;
int n;
int e;
int th;
failfish *head;
failfish *tail;
} failfish_queue;
//The next stuct will help with each pond created
typedef struct failfish_pond
{
int pond_number;
struct failfish_queue *queue; /*The pond will have queue.*/
} failfish_pond;
//The next piece of my program will is a cicular link list. Prototype failfish will create failfish pre-request of the assignment.
failfish *create_failfish(int sequence_number)
{
failfish *f = malloc(sizeof(failfish));
f->sequence_number = sequence_number;
f->next = NULL;
return f;
}
//The next code will create the queues for the failfishs monster.
failfish_queue *create_failfish_queue(char *pondname, int n, int e, int th)
{
failfish_queue *q = malloc(sizeof(failfish_queue));
q->head = NULL;
q->tail = NULL;
q->pondname = strdup(pondname);
q->n = n;
q->e = e;
q->th = th;
return q;
}
//Number of failgroup in line.
int get_num_failgroup(FILE *ifp)
{
char check[100];
char *num_check_string;
int num_check;
//error is here
num_check_string = fgets(check, 99, ifp);
num_check = atoi(num_check_string);
printf("The amount of failfish is %d ", num_check);
return num_check;
}
//Here I created an array for the ponds.
failfish_pond *create_pond_array(int num_check)
{
char *pondname;
int pond_number;
int n;
int e;
int th;
failfish_pond *fp = malloc(num_check * sizeof(failfish_pond));
fp->queue = malloc(sizeof(fp->queue));
fp->queue = malloc(sizeof(failfish_queue));
fp->queue->pondname = strdup(pondname);
//found error here.
fp->pond_number = pond_number;
fp->queue = create_failfish_queue(pondname, n, e, th);
return fp;
}
void file_input(FILE *ifp, int num_check, failfish_queue *q, failfish_pond *fp)
{
int pond_number;
int r;
int n;
int e;
int th;
char check[100];
for (r = 0; r < num_check; r++)
{
fscanf(ifp, "%d", &fp[r].pond_number);
printf("Pond Number: %d ", fp[r].pond_number);
// fgets(check, 99, ifp);
q[r].pondname = malloc(sizeof(char) * 100);
fscanf(ifp, "%s", q[r].pondname);
printf("Name: %s ", q[r].pondname);
fscanf(ifp, "%d", &q[r].n);
printf("%d ", q[r].n);
fscanf(ifp, "%d", &q[r].e);
printf("%d ", q[r].e);
fscanf(ifp, "%d", &q[r].th);
printf("%d ", q[r].th);
fp[r].queue = &q[r];
printf(" ");
}
}
void show_pond(failfish_pond *fp, int num_check)
{
int r;
for (r = 0; r < num_check; r++)
{
printf("New Pond: ");
printf("Number: %d ", fp[r].pond_number);
printf("Name: %s ", fp[r].queue->pondname);
printf("Number of fish: %d ",
fp[r].queue->n);
printf("Number of fish eaten: %d ", fp[r].queue->e);
printf("Threshold: %d ", fp[r].queue->th);
printf(" ");
}
printf(" ");
}
void qlink(failfish *f, failfish_pond *fp)
{
if (fp->queue->head == NULL)
{
fp->queue->head = f;
fp->queue->tail = f;
f->previous = f;
f->next = f;
}
else
{
printf("Program Error/n");
f->previous = fp->queue->tail;
f->next = fp->queue->head;
fp->queue->head->previous = f;
fp->queue->tail->next = f;
fp->queue->head = f;
}
}
void pondSorting(failfish_pond *fp, int num_check)
{
int i;
int r;
int m_idx;
struct failfish_pond temp;
for (i = 0; i < num_check; i++)
{
m_idx = i;
for (r = 0; r < (num_check - 1); r++)
{
if (fp[r].pond_number > fp[m_idx].pond_number)
{
m_idx = r;
temp = fp[m_idx];
fp[m_idx] = fp[i];
fp[i] = temp;
}
}
}
show_pond(fp, num_check);
}
void fish_status(failfish *f, failfish_pond *fp, int num_check)
{
int r;
int j;
printf("Checking pond status. Please wait.... %d ", num_check);
for (r = 0; r < num_check; r++)
{
printf("%d ", fp[r].pond_number);
printf("%s ", fp[r].queue->pondname);
for (j = 1; j < fp[r].queue->n + 1; j++)
{
f = create_failfish(j);
printf("%d ", j);
// this is an invalid operation
f[r].sequence_number = j;
}
printf(" ");
}
printf(" ");
}
void get_rid_of_fish(failfish *f)
{
free(f);
}
void clear_link(failfish *eaten_fish, int dispose)
{
if (dispose != 0)
{
get_rid_of_fish(eaten_fish);
}
else
{
eaten_fish->next = NULL;
eaten_fish->previous = NULL;
}
printf("Completed...");
}
void delete_fish(failfish *f, failfish *eaten_fish, failfish_pond *fp, int dispose)
{
f = fp->queue->head;
if (eaten_fish->next == eaten_fish)
{
clear_link(eaten_fish, dispose);
fp->queue->head = NULL;
fp->queue->tail = NULL;
return;
}
if (fp->queue->head == eaten_fish)
{
fp->queue->head = eaten_fish->next;
}
if (fp->queue->tail == eaten_fish)
{
fp->queue->tail = eaten_fish->previous;
}
eaten_fish->previous->next = eaten_fish->next;
eaten_fish->next->previous = eaten_fish->previous;
clear_link(eaten_fish, dispose);
}
void course_one(failfish *f, failfish_pond *fp, int num_check)
{
int i;
int r;
int j;
int dispose;
failfish *eaten_fish;
failfish *head;
failfish *tail;
failfish *traverse = fp->queue->head;
printf("Course number one ");
printf("cdc %d ", num_check);
for (i = 0; i < num_check; i++)
{
printf("Pond %d: %s ", fp[i].pond_number, fp[i].queue->pondname);
printf("Pond %d: fp[i].queue->n %i ", fp[i].pond_number, fp[i].queue->n);
printf("Pond %d: fp[i].queue->th %i ", fp[i].pond_number, fp[i].queue->th);
if (fp[i].queue->n != fp[i].queue->th)
{
for (j = fp[i].queue->n; j > fp[i].queue->th; j--)
{
for (r = 0; r < (fp[i].queue->e - 1); r++)
{
printf("This worked ");
traverse = traverse->next;
printf("made it here ");
}
}
}
if (fp[i].queue->n != fp[i].queue->th)
{
for (j = fp[i].queue->n; j > fp[i].queue->th; j--)
{
for (r = 0; r < (fp[i].queue->e - 1); r++)
{
traverse = traverse->next;
delete_fish(f, eaten_fish, fp, dispose);
fp[i].queue->n = fp[i].queue->n - 1;
printf("FailFish %d eaten ", f[j].sequence_number);
}
printf(" Failfish %i eaten", traverse->sequence_number);
if (head == traverse)
{
head = head->next;
}
delete_fish(f, traverse, fp, dispose);
}
}
}
}
//The main of the program
int main(void)
{
FILE *ifp;
FILE *ofp;
int num_check;
int sequence_Num;
char pondname[128];
int n;
int e;
int th;
int i;
int fish_high;
failfish_queue *fqueue[10];
failfish_pond *fpond;
failfish *ffish;
ifp = fopen("input.txt", "r");
ofp = fopen("output.txt", "w");
// sequence_Num is uninitialized
ffish = create_failfish(sequence_Num);
num_check = get_num_failgroup(ifp);
fpond = create_pond_array(num_check);
file_input(ifp, num_check, fqueue, fpond);
pondSorting(fpond, num_check);
fish_status(ffish, fpond, num_check);
course_one(ffish, fpond, num_check);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started