Home > Mobile >  Scanf an unknown amount of integers ,and stop the procedure while get an '\n'
Scanf an unknown amount of integers ,and stop the procedure while get an '\n'

Time:10-04

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{

    int a[100];
    char mi[200];
    char delims[] = "\040";
    char * li;

    fgets(mi,200,stdin);
    li = mi;
    li = strtok(li,delims);
    a[0] = atoi(li);
    for(int i=1;li!=NULL;i  )
    {
        li = strtok(NULL,delims);
        a[i] = atoi(li);
    }

    return 0;
}

all the input were separate by space. I wrote one,but it's so complex,so I wonder if there is an easier way.

CodePudding user response:

That is pretty much how it needs to be done, although you have a small problem with your tokenizing loop: think about what will happen when there are no more tokens remaining. I would recommend using more meaningful variable names, to make code more self-documenting.

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

int main(void)
{

    int values[100];
    char linebuf[200];
    char *token;

    fgets(linebuf,sizeof(linebuf),stdin);
    token = strtok(linebuf,' ');
    for(int i = 0; token; i  ) {
        values[i] = atoi(token);
        token = strtok(NULL,' ');
    }

    return 0;
}

If it were my code, I might prefer to use strtol instead of atoi, to be able to have some more error checking to screen out extraneous (non-numerical) characters.

CodePudding user response:

If you use strtol instead of atoi, you don't need to use strtok. The function strtol will automatically discard all leading whitespace characters and will tell you the first character that it didn't convert, so you know where to continue.

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

#define MAX_NUMBERS 100

int main(void)
{
    long numbers[MAX_NUMBERS];
    char line[200];
    char *p;

    fgets( line, sizeof line, stdin );
    p = line;

    for ( int i = 0; i < MAX_NUMBERS; i   )
    {
        long ret;
        char *q;

        numbers[i] = strtol( p, &q, 10 );
        if ( p == q )
            break;

        p = q;
    }

    return 0;
}

If you do proper error checking, and print the result, the code will look like this:

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

#define MAX_NUMBERS 100

int main(void)
{
    long numbers[MAX_NUMBERS];
    int num_converted;
    char line[200];
    char *p;
    int i;

    //attempt to read one line of input
    if ( fgets( line, sizeof line, stdin ) == NULL )
    {
        fprintf( stderr, "error reading line!" );
        exit( EXIT_FAILURE );
    }

    //make sure that entire line was read
    if ( strchr( line, '\n' ) == NULL )
    {
        fprintf( stderr, "input line was too long!" );
        exit( EXIT_FAILURE );
    }

    //begin at the start of the line
    p = line;

    for ( i = 0; i < MAX_NUMBERS; i   )
    {
        long ret;
        char *q;

        //attempt to convert one number
        errno = 0;
        numbers[i] = strtol( p, &q, 10 );

        //break loop if it was unable to convert another number
        if ( p == q )
            break;

        //verify that the found number is not out of range
        if ( errno == ERANGE )
        {
            fprintf( stderr, "number out of range!" );
            exit( EXIT_FAILURE );
        }

        //continue after the converted number
        p = q;
    }

    //remember how many numbers were converted
    num_converted = i;

    //print how many numbers were converted
    printf( "Converted %d numbers:\n", i );

    //print all the converted numbers
    for ( i = 0; i < num_converted; i   )
        printf( "%ld\n", numbers[i] );

    return 0;
}

If you run this code with the input 761 87 2387, followed by a newline character, then the output will be the following:

Converted 3 numbers:
761
87
2387
  • Related