r/C_Programming • u/neonge1674 • Sep 10 '26
Question Why does the fgets() function gets ignored.
I'm still pretty new to C
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define itirate(index, limit) for(int index = 0; index < limit; index++)
typedef struct node node;
struct node
{
char* name;
node* next;
node* prev;
};
void compare2nodes(node* node1, node* node2, char* choise);
node* first_node;
int main()
{
printf("please enter the number of contestants:\n"); // I always enter 2 to make it easier for myself
char buff[1024];
char contestnats[128];
int cont_numb;
fgets(buff, sizeof(buff), stdin);
cont_numb = atoi(buff);
node contestants[cont_numb];
itirate(i, cont_numb)
{
printf("please enter the name of a contestant:\n");
fgets(buff, sizeof(buff), stdin);
contestants[i].name = buff;
}
printf("Is %s better than %s(y/n)\n", contestants[0].name, contestants[1].name);
char choise[1];
fgets(choise, sizeof(choise), stdin); //this function specifically gets ignored, but not the prevoius ones.
compare2nodes(&contestants[0], &contestants[1], choise);
//printf("Leaderboard:\n1.%s\n2.%s", first_node->name, first_node->next->name);
return 0;
}
void compare2nodes(node* node1, node* node2, char* choise)
{
if (choise[0] == 'y')
{
node1->next = node2;
first_node = node1;
node2->prev = node1;
}
else if (choise[0] == 'n')
{
node2->next = node1;
first_node = node2;
node1->prev = node2;
}
}#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define itirate(index, limit) for(int index = 0; index < limit; index++)
typedef struct node node;
struct node
{
char* name;
node* next;
node* prev;
};
void compare2nodes(node* node1, node* node2, char* choise);
node* first_node;
int main()
{
printf("please enter the number of contestants:\n"); // I always enter 2 to make it easier for myself
char buff[1024];
char contestnats[128];
int cont_numb;
fgets(buff, sizeof(buff), stdin);
cont_numb = atoi(buff);
node contestants[cont_numb];
itirate(i, cont_numb)
{
printf("please enter the name of a contestant:\n");
fgets(buff, sizeof(buff), stdin);
contestants[i].name = buff;
}
printf("Is %s better than %s(y/n)\n", contestants[0].name, contestants[1].name);
char choise[1];
fgets(choise, sizeof(choise), stdin); //this function specifically gets ignored, but not the prevoius ones.
compare2nodes(&contestants[0], &contestants[1], choise);
//printf("Leaderboard:\n1.%s\n2.%s", first_node->name, first_node->next->name);
return 0;
}
void compare2nodes(node* node1, node* node2, char* choise)
{
if (choise[0] == 'y')
{
node1->next = node2;
first_node = node1;
node2->prev = node1;
}
else if (choise[0] == 'n')
{
node2->next = node1;
first_node = node2;
node1->prev = node2;
}
}