I am using fgets
inside another function to return valid data, as I have a menu list and it becomes repetitive to write out fgets
for each function. I pass through a character array result[]
which seems to always be the sizeof 8, which is causing my data to get clipped.
void get_input(char result[]) {
if(fgets(result, sizeof(result), stdin) == NULL) { // sizeof is ALWAYS 8!
read_rest_of_line();
printf("\n");
} else
clearbuffer(result);
}
void get_valid_input(char result[]) {
get_input(result);
while(result == NULL || result[0] == '\n') {
printf("Enter a valid input or quit with CTRL+D\n");
get_input(result);
}
/* remove newline replace with nul terminator */
result[strlen(result)-1] = '\0';
}
It being executed:
char equipment_id[7];
char equipment_name[31+1];
char equipment_quantity[sizeof(long)];
long quantity;
printf("New ID: ");
get_valid_input(equipment_id);
printf("Please enter the equipment name: ");
get_valid_input(equipment_name);
printf("Please enter the total quantity: ");
get_valid_input(equipment_quantity);