I am receiving input that I should be putting into an array, and although I've already implemented methods to check if the array is too short/long, I cannot seem to get around to checking if the array contains non-numeric characters.
The array is separated by whitespace and always ends with EOF, and I have that part figured out too. Here are some methods I've tried but could not solve the issue. Am I doing something wrong with my code?
char *str;
long nums;
fgets (line, BUFFER, stdin);
nums = strtol(line, &str, 10);
if (str != '\0'){
printf("Invalid input z");
return 1;
}
//but line here only returns the first value before whitespace, which is != to EOF
My method of converting the input from fgets into input is by using strtok, before using atoi to convert it to an integer before storing it in the array. Is there a better/easier method that works that I'm missing here? Thanks!
EDIT: Here is how I am doing the array
int count = row * col;
for (int i = 0; i < row && !stop; i++){
for (int j = 0; j < col && !stop; j++){
num = strtok(NULL, " ");
if (num == NULL){
printf("Invalid input 1");
stop = true;
}else{
int curr = atoi(num);
grid[i][j] = curr;
}
}
}