0

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;
        }
    }
}
William
  • 81
  • 5
  • Have you looked at how `isdigit` is implemented? – agbinfo Feb 20 '20 at 18:31
  • Doesn't it just check if a character is a number and returns 0 if it's not? The issue I'd have here is that my array can be initialized with 0's, but although I could use isalpha, I think that we aren't allowed to use either function – William Feb 20 '20 at 18:34
  • `isdigit` does not check for the *value* `0` to `9` but the *characters* `'0'` to `'9'`. – Weather Vane Feb 20 '20 at 18:39
  • `if (str != '\0')` is incorrect, it checks the pointer value. You need `if (*str != '\0')`. But anyway, `fgets` retains the ending newline, so the next character won't be the string terminator. You can remove it, please see [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Feb 20 '20 at 18:41
  • I tried *str, but now it always has EOF, and my function doesn't work when there are letters in the array – William Feb 20 '20 at 18:54
  • If your numbers are separated by spaces, shouldn't you check `*str != ' ' && *str !='\0'`? – agbinfo Feb 20 '20 at 18:55
  • That doesn't fix my problem too, I also tried moving it inside my loops where I add in the values of the array, but it still doesn't work. It was originally outside the for loops – William Feb 20 '20 at 19:04
  • Works fine here: https://ideone.com/YblhAf – agbinfo Feb 20 '20 at 19:05
  • I'm not sure if theres a difference between c and c++, but an example input would be 2 2 0 0 0 0, where the first two numbers are rows and columns followed by the array. If the array is 0 0 a 0 instead of 0 0 0 0, my program is unable to distinguish between the two – William Feb 20 '20 at 19:13
  • 1
    Your title "... check if a users input is a string ..." doesn't say what you're trying to say. The input *is* a string. You want to check whether the string contains certain characters (and you haven't stated clearly just what you're checking for). I *think* you want to verify that the string contains only decimal digits. If so, please update your question to say that explicitly. – Keith Thompson Feb 20 '20 at 19:37
  • William, Recall that `fgets (line, BUFFER, stdin);` also saves the `'\n'`. Your code does not account for that character. – chux - Reinstate Monica Feb 20 '20 at 19:47

1 Answers1

1

Here's a quick example of what I think you're trying to do.


int main() {
    char line[1000];

    char *str;
    long nums;
    fgets (line, 1000, stdin);
    char *next = strtok(line, " \n");
    while (next)
    {
        nums = strtol(next, &str, 10);
        if (*str != '\0') {
            printf("Invalid input %s\n", next);
            return 1;
        }
        printf("Found %ld\n", nums);
        next = strtok(0, " \n");
    }

    return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
agbinfo
  • 793
  • 5
  • 17