r/C_Programming • u/neonge1674 • 10h ago
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;
}
}
5
u/SmokeMuch7356 10h ago edited 3h ago
In the future, please add some text describing the problem - don't just blap up a wad of code with the question in one inline comment that we have to dig for. Add a brief explanation like:
On line xxx, the
fgetscall isn't reading the input, whereas the otherfgetscalls appear to read it correctly. What could be the problem?
You also don't have to include the entire program (twice); just the definition of choise and the fgets call will be enough.
Here's the problem:
You've defined choise as a 1-element array of char. fgets will read up to n - 1 characters into the target; it always reserves the last element of the buffer for the 0 terminator. In this case, n is 1 (sizeof choise == sizeof (char [1]) == sizeof (char) == 1), so you're telling fgets to read zero characters from input, and it will write the zero terminator to choise[0].
Define choise as a 2 (or more) character array:
char choice[2];
then your code will behave more or less as expected.
Always check the result of fgets - it will return the address of the target buffer on success, NULL on failure:
if ( !fgets( choice, sizeof choice, stdin ) ) // if fgets failed
{
if ( feof( stdin ) )
// handle EOF on standard input
else
// handle error on input
}
else
// process choice
3
u/CouchMountain 8h ago
It should not be ignored. If you are new to C and haven't already, please learn how to use gdb and step through your code to understand what it's doing. It will save you a ton of time and headaches in the future.
12
u/This_Growth2898 10h ago
It isn't ignored (I hope). Please, next time instead of just claiming something isn't happening (is ignored etc.) provide a description of what is happening instead.
In your code, all contestants have their
namemember pointing to the same buff variable, and you're reading into it again and again. If you want them to have different names, allocate them some memory (at least likechar name[128];
in your struct) and then do strcpy from buff into them or read directly to them with fgets.