Thread: Scan a line of integers but skip spaces

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    23

    Scan a line of integers but skip spaces

    Hey, I'm having trouble with my program. The user is going to input a chain of ints and I need to scan these into an array. But the problem is I need to whenever there is a space I need it to scan into the next arrays value. like this...
    they input 1 2 3 12 43
    this would mean that the array would get the values:
    a[0]=1
    a[1]=2
    a[2]=3
    a[3]=12
    a[4]=43

    Does anyone know how I would do this?
    Thanks,
    Curtis

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    What have you tried?

    There are a lot of ways to do so. I ll give you my preferred one. Read one-by-one characters to a temporary array. If it is greater than '9' or less than '0' then use atoi() and store its value to the array. Repeat.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's what scanf already does.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    23
    Wouldn't it scan the whole thing into just a[0]?
    I'm not sure whether or not it will keep track of the spaces compared to which slot of the array it needs to be stored in.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Creedy View Post
    Wouldn't it scan the whole thing into just a[0]?
    That's an easy thing to check:
    Code:
    #include <stdio.h>
    int main(void) {
        int a[5];
        scanf("%d", &a[0]);
        printf("You entered %d\n", a[0]);
        return 0;
    }
    Type in a bunch of numbers and see what happens.
    Quote Originally Posted by Creedy View Post
    I'm not sure whether or not it will keep track of the spaces compared to which slot of the array it needs to be stored in.
    To expand on what I meant, you would presumably use fgets or something similar to read in an entire line, then use sscanf to read from that line in a loop. (If you put plain old scanf in a loop, then your user would have to hit EOF or something similar to make the loop stop, or type in a special "sentinel" value, or just generally do something special to make the reading stop.)

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by tabstop View Post
    That's what scanf already does.
    True, but still not my preferred method. Not that I have a strong preference...

  7. #7
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    If you were sure your user was guaranteed to put in exactly 5 integers, scanf is your friend. Any deviation and you are up a creek. Me, I would read in the whole like, use strtok to parse the line on ' ' and run each substring through atoi().
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Imposing Line Numbers automatically in the C code
    By cavestine in forum C Programming
    Replies: 14
    Last Post: 10-15-2007, 12:41 AM
  2. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  3. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM
  5. inputting line of text vs. integers in STACK
    By sballew in forum C Programming
    Replies: 17
    Last Post: 11-27-2001, 11:23 PM