Using the Scanf function for inputs for different lengths

Viewed 289

I want to use the scanf function to get inputs that are either two numbers, or one number from the user and put them in an array. However, I'm not sure how I can use this same function to get inputs for both one element of the array, and also two elements of the array.

i.e. if the user enters, 9 0, it should be able to successfully store that in an array and move onto new code, or if the user enters something like 1, it should also be able to successfully store that in the array, and move onto new code.

I've tried putting the scanf into a while loop:

int scanned_array[2] = {};
int element = 0;

while(scanf("%d", &scanned_array[element]) {
    //... more code here which will have different functions depending on the input...
    element++;
}

How can I successfully do this while only using the scanf function, while loops, arrays and if statements?

**Edit: Just wondering, if I changed my code according to what was suggested below by @nmgari, how could I go to if(num == 1) { ... without having to press ctrl+d?

Thanks for reading!

3 Answers

If i understand correctly you want to call scanf() once and read either one number or two.

Something like this should work:

int scanned_array[2];
int num_input = 0;

num_input = scanf("%d %d", &scanned_array[0], &scanned_array[1]); 

if(num_input == 1)
{
    //Do something
}
else if (num_input == 2)
{
    //Do somethine else
}

Anyway, you need to consider that the second element in the array may never get a value.

You should read the input line with fgets() and parse it with sscanf():

    char buf[120];
    if (fgets(buf, sizeof buf, stdin)) {
        int i1, i2;
        switch (sscanf(buf, "%d%d", &i1, &i2)) {
          case 2:
            /* the user entered 2 integers */
            handle_2_integers(i1, i2);
            break;
          case 1:
            /* the user entered a single integer */
            handle_1_integer(i1);
            break;
          default:
            /* the user did not enter a number */
            handle_invalid_input();
            break;
        }
    } else {
        /* the input stream is at end of file or encountered an error */
        handle_end_of_file();
    }

If you run scanf("%d%d", &i1, &i2), scanf() will keep reading the input stream until either end of file, or 2 integers have been read or an character has been input that cannot be converted to an integer, possibly reading multiples lines of input for one of these cases to occur. If you want to handle a single line of input, you cannot use scanf() directly.

A separate scan for whitespace could be used.
The scanset %1[ \t\r\n] will capture whitespace character by character. The loop will exit on non-whitespace or a newline.
Then try to scan an integer.

#include <stdio.h>
#include <stdlib.h>

int main ( void) {
    char space[2] = "";
    int scanned = 0;
    int scanned_array[2] = { 0};

    while ( 1) {
        while ( 1 == scanf ( "%1[ \t\r\n]", space)) {
            if ( '\n' == space[0]) {
                break;
            }
        }
        if ( '\n' == space[0]) {
            break;
        }
        if ( 1 != scanf ( "%d", &scanned_array[scanned])) {
            fprintf ( stderr, "bad input\n");
            return 1;
        }
        ++scanned;
        if ( 2 <= scanned) {
            break;
        }
    }

    for ( int each = 0; each < scanned; ++each) {
        printf ( "%d\n", scanned_array[each]);
    }

    return 0;
}
Related