I compile my code with "gcc -O -Wall -Werror" and after seraching through the forums i tried to make the fgets function void as well.I initially tried without void. Nothing seems to work. The top line is the specific line of code that isn't working.
(void) fgets(diceString, 300, stdin);
diceString[strlen(diceString) - 1] = '\0';
The solution of checking it with the following also didn't work as per the similar question.
if(fgets(diceString, 300, stdin) ){
I will also provide the context of the entire code below
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define INCORRECT_FORMAT 0
#define OUT_OF_RANGE 1
#define INC_NUM_VALUES 2
#define TRUE 1
#define FALSE 0
int main(int argc, char *argv[]){
char diceString[300] = {0};
int diceNum[5] = {0};
int errorType[3] = {0};
int valueCounter = 0;
(void) fgets(diceString, 300, stdin);
diceString[strlen(diceString) - 1] = '\0';
int i = 0;
int j = 0;
char ch = 0;
while(diceString[i] != '\0'){
if(diceString[i] <= '6' && diceString[i] >= '1'){
ch = diceString[i];
diceNum[j] = ch - '0';
j++;
valueCounter++;
}
if(valueCounter >5){
errorType[INC_NUM_VALUES] = TRUE;
}if( i < 4 && diceString[i+1] != ' '){
errorType[INCORRECT_FORMAT] = TRUE;
}if( i > 0 && diceString[i-1] != ' '){
errorType[INCORRECT_FORMAT] = TRUE;
}
i++;
}
printf("The errors are :\n");
i = 0;
while(i < 3){
printf("%d", errorType[i]);
i++;
}
i = 0;
while(i<5){
printf("%d\n",diceNum[i]);
i++;
}
return 0;
}