3

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;
}
Amith Kamath
  • 31
  • 1
  • 3
  • Remove casting to void before the function call (void). – Vlad from Moscow Sep 02 '16 at 13:15
  • 3
    You could always check the return value to make sure that there wasn't an error `fgets`ing a string. – rjp Sep 02 '16 at 13:18
  • i tried without the void first. I added the void after some research on why this was happening. What does check the return value mean in this context? – Amith Kamath Sep 02 '16 at 13:25
  • 1
    @AmithKamath, the return value of `fgets()` tells you about whether an error was encountered, and whether zero bytes were read as a result of encountering EOF. If either occurs, then the provided array may not have been filled with the expected data. Instead of assuming that that never happens, your program should check the function's return value and take alternative action (maybe terminating with a diagnostic message) if it ever does happen. – John Bollinger Sep 02 '16 at 13:35
  • 1
    1) Better to check to return value of `fgets()` like `if (fgets(diceString, sizeof diceString, stdin) == NULL) return -1;` 2) `diceString[strlen(diceString) - 1] = '\0` is a hacker exploit. Use [this](http://stackoverflow.com/a/28462221/2410359) – chux - Reinstate Monica Sep 02 '16 at 13:35

0 Answers0