Thread: Space separated input in C

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    1

    Space separated input in C

    I want to make a program that allows the use to input space separated numbers then read those numbers into an array. I wrote a program which works for all cases except when the user inputs data that is separated by more than 1 space or has multiple trailing spaces.

    Sample input and output:

    Code:
    Input:"0 1 2 3 4 5 6 7 8 9"
    Output: [0,1,2,3,4,5,6,7,8,9]
    
    Input:"0 1 2     3   4    5  6        7   8       9        "
    Output: [0,1,2,3,4,5,6,7,8,9]
    
    Input:"0123456789"
    Output: Error: Bad input format.
    
    Input:"0 1 24 3 5 12 4 6"
    Output: Error: Bad input format.
    
    Input:"0 1 2.3 4 5 6,7"
    Output: Error: Bad input format.
    
    Input:"0 1 2 -3 4 -5 6 7"
    Output: Error: Bad input format.
    
    Input:"0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9"
    Output: Error: Too many numbers.
    Code:
        int input_array[10] = { 0 };
        int size = 0;
        int ch1 = 0, ch2 = 0;
    
        while (ch1 != '\n' && ch2 != '\n') {
            ch1 = getchar();
            if (ch1 == EOF || ch1 == '\n' || !isdigit(ch1)) {
                printf("Error: Bad input format.");
                exit(EXIT_FAILURE);
            }
            ch2 = getchar();
            if (ch2 != EOF && ch2 != '\n' && ch2 != ' ') {
                printf("Error: Bad input format.");
                exit(EXIT_FAILURE);
            } else {
                input_array[size] = ch1 - '0';
                size+=1;
            }
            if ((ch1 == '\n' || ch2 == '\n') && size != DICE_SIZE) {
                printf("Error: Too many numbers.");
                exit(EXIT_FAILURE);
    
            }
        }
    The code above works for all cases except when the inputs are not separated by exactly 1 space with no trailing or leading spaces, so I get an error for the following input but

    Code:
    Input:"0 1 2     3   4    5  6        7   8       9        "
    Output: Error: Bad input format.
    Desired Output: [0,1,2,3,4,5,6,7,8,9]
    I modified the code above and now it works for this input "0 1 2 3 4 5 6 7 8 9 " but it stopped working for "0123456789", i.e. I get:

    Code:
    Input:"0123456789"
    Output: [0,1,2,3,4,5,6,7,8,9]
    Desired Output: Error: Bad input format.
    Last edited by user2357; 09-03-2016 at 07:06 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your loop should read only one character per iteration.
    Otherwise, you have too many combinations of characters to deal with.

    Eg
    Code:
    while ( (ch1 = getchar()) != EOF ) {
      if ( ch1 == '\n' ) ...
      else if ( ch1 == ' ' ) ...
      else if ( isdigit(ch1) ) ...
      else if ....
      else ...
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    1
    Code:
    char line [50];
    fgets(line, sizeof(line), stdin);
    int i=0;
    int array[50];
    for(i=0; i<49;   i){
    sscanf(line, "%d", array[i]);
    }

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Le Pastore View Post
    Code:
    char line [50];
    fgets(line, sizeof(line), stdin);
    int i=0;
    int array[50];
    for(i=0; i<49;   i){
    sscanf(line, "%d", array[i]);
    }
    Don't post code without testing it. You forgot the ++ after the i, you presumably meant to say i<50 (although what if there's only 10 numbers?), and even if you fixed those errors, it won't work anyway since the sscanf would keep reading the first number on the line.

    And even if you fixed that, it still doesn't answer the actual question that was asked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer input till space.
    By peripatein in forum C Programming
    Replies: 3
    Last Post: 05-24-2013, 02:20 AM
  2. Read inputs separated by space
    By acpower in forum C Programming
    Replies: 2
    Last Post: 04-10-2012, 09:54 AM
  3. Obtain space separated values from text file
    By mammoth in forum C Programming
    Replies: 3
    Last Post: 04-20-2011, 04:49 AM
  4. space between input
    By dNNNY in forum C Programming
    Replies: 8
    Last Post: 07-15-2008, 12:52 PM
  5. Read two words separated by a space with scanf at once
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 06-23-2005, 04:17 PM

Tags for this Thread