0

I want to get numbers from user in a single line for example:

2 1 2 3 4 

The first number: 2 means that my Matrix should be with size of N and the next 4 numbers should insert into my Matrix (Matrix dimension should be ).

The thing is that if wrong number of numbers inserted for example: 2 1 2 3

After the number 2 i expected 4 numbers and here i have only 3 so in this case i want to break and currently this is not the case here.

int dimension, num;
int *mat;

printf("Please enter numbers: ");
scanf("%d", &num);
int matIndex = 0;

/* Set Matrix dimension. */
dimension = num;
if (dimension < 2)
{
    printf("Size must be posiitve integer and bugger then 1.");
    return 1;
}
else
{
    mat = malloc(dimension * dimension * sizeof *mat);
    if (mat == NULL) {
        fprintf(stderr, "%s\n", "Error in malloc");
        exit(1);
    }
}

// All the numbers will be taken as elements of the dynamically allocated array,
for (int i = 0; i < dimension*dimension; i++)
    scanf("%d", &mat[i]);
Dana Yeger
  • 617
  • 3
  • 9
  • 26
  • 1
    You can check the answer I provided there.. You should check the return type of `scanf` – user2736738 Nov 24 '17 at 16:45
  • You should take input as a string, and then check if input have enough inputs or not. – Jay Joshi Nov 24 '17 at 16:46
  • 1
    If you need to accommodate expected input items being altogether missing, then `scanf` is not well suited to the job. I suggest reading the input one line at a time via `fgets()` (be sure to read its docs carefully), and extracting the numbers via `sscanf()` on the string. Do, futhermore, check the return values of these functions to detect error conditions, including, but not limited to, the line read from the input containing fewer fields than you expected. – John Bollinger Nov 24 '17 at 16:50
  • Anyone having any constructive criticism to my answer that I gave to OP earlier please check https://stackoverflow.com/questions/47473323/efficient-way-to-get-numbers-from-user-and-fill-matrix – user2736738 Nov 24 '17 at 16:52
  • And what if there are one or more space characters between the last number on a line and the newline, @user3121023? – John Bollinger Nov 24 '17 at 17:05
  • I think you missed the point, @user3121023. The scenario I described presents no problem for number parsing; rather, it presents a problem for recognizing that numbers are missing from the line. – John Bollinger Nov 24 '17 at 17:12

1 Answers1

-1

Take dimension as integer first, then take whole input as a string. Then check if the string has exact number of inputs by using strtok.

here is the code:

//after the dimension is scanned and verified successfully.
 char input[200];
 char *token;
 char temp;

 fflush(stdin);
 scanf("%199[0-9 ]",&input[0]);
 // take all inputs as a string.    

 token = strtok(input, " ");
 while( token != NULL )
 {
      top++;
      mat[top] = atoi(token);

      token = strtok(NULL, " ");

 }

if(top < dimension * dimension -1)
{
    printf("Not enough inputs.!");
}
else if(top > dimension * dimension -1)
{
    printf("input limit exceeded");
}
else
{
    // Input is correct.!

    //  . . .
}
Jay Joshi
  • 868
  • 8
  • 24
  • 3
    Note that `fflush(stdin)` causes undefined behavior, according to the Standard. Some systems do define behavior for this, but relying on this is not portable. There are better, and portable, ways to clear the input stream. – ad absurdum Nov 24 '17 at 19:43
  • I am sorry but i am not aware of different ways to flush the input, Can you please give suggestions.? – Jay Joshi Nov 24 '17 at 19:47
  • 1
    The canonical method is to use `int ch; while ((ch = getchar()) != '\n' && ch != EOF) { continue; }`. This only works as expected if there is at least one character in the input stream, which is typically the case after using `scanf()`. This can also be used after `fgets()` to clear remaining input from the input stream (only if there is remaining input). You might [have a look at this](https://stackoverflow.com/questions/40958298/is-this-the-proper-way-to-flush-the-c-input-stream). – ad absurdum Nov 24 '17 at 19:53
  • I appreciate the suggestions, but i already run the program for many test cases and it works perfect. after practice i came to know the space in `%[0-9 ]` does not include new line character. so whenever I press enter, it gives the output. (print result in my case). There is no issue like hanging the program when i press enter. – Jay Joshi Nov 24 '17 at 21:05
  • 1
    @JayJoshi: Nobody doubts it works *on your system*. The problem is that it will not work like you expect on *other* systems, and it is ill advised to advertise non-portable behaviour without at least a caveat. And where portable solutions exist -- such as here -- they should be preferred. – DevSolar Nov 24 '17 at 21:49