简体   繁体   中英

Reading multiple lines of input and save ints into array

I am trying to save some int s into an array that are read from the user input . I don't know the number of int s on each line, only the number of lines which will also be the maximum number of int s on that line. For example if this is 5 then the user should input 5 lines with int s and each line a maximum number of 5 elements on it. The values will be positive. What am I doing wrong?

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

int main(int argc, char *argv[]) {
    int n;
    scanf("%d",&n);
    int array_row[n];
    int i=0;
    int noEnter=n;
    //My idea is when in getchar() there is a enter it means that the user wants to go to the next line so decrement noEnter with 1 and also store a value -1 which tells me that that was the end of the line
    while(noEnter!=0){
        int c;
        if(scanf("%d",&c)==1){
            array_row[i]=c;
            i++;
            continue;
        }
        char d=getchar();
        if(d=='\n'){
            array_row[i]=-1;
            i++;
            noEnter--;
            continue;
        }
    }



    for(int i=0;i<n*n;i++){
        printf("%d ",array_row[i]);
    }

    return 0;
}

Example of input:

5
4
4 35 65
4 32
2 222 4 5 6
4

Output:

4 -1 4 35 65 -1 4 32 -1 2 222 4 5 6 -1 4 -1

scanf doesn't stop at \\n as you are expecting. It is reading the integers..as a result I guess your program is not even ending. Read a line and divide it into integers. That way you can get the integers and process them accordingly.

As the number of integers entered is not known before hand you can tokenize the line strtok and then convert each of the numbers from string to int .

Moreover your input is not conforming to the output given. You have given the input 5 in first line but it never appeared in the output on the first few numbers.

#define LINE 100
..

int len=0;
char line[LINE];
char*word;
while(noEnter<5){
    fgets(line,LINE,stdin);
    word=strtok(line," \n");
    if(word == NULL) 
         break;
    while(word != NULL){
        int p=atoi(word);
        array_row[len++]=p;
        word=strtok(NULL," \n");
    }
    noEnter++;
    array_row[len++]=-1;
}

scanf is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read.

Here you are not even getting the chance of consuming the \\n with a getchar() before reaching that line the \\n is consumed and discarded by the scanf() .

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE 50
int main(int argc, char *argv[]) {
    int n=5;
    int array_row[LINE];
    int len=0;
    int noEnter=3;
    char line[LINE];
    char*word;
    while(noEnter<5){

        fgets(line,LINE,stdin);
        word=strtok(line," \n");
        if(word == NULL)
           break;
        while(word != NULL){
            int p=atoi(word);
            array_row[len++]=p;
            word=strtok(NULL," \n");
        }
        noEnter++;
        array_row[len++]=-1;
    }
    for(int i=0;i<len;i++){
       printf("%d ",array_row[i]);
    }

    return 0;
}

One major problem with scanf is that it is not useful for reading data where the location of newlines (as opposed to other whitespace) is important. Scanf just breaks things on whitespace with no difference between spaces, tabs, and newlines, so there's no difference between a line with 5 numbers and 5 lines each with one number.

As a result, if you care about newlines, you generally will need to read lines into a buffer with fgets and then use sscanf to parse the numbers in that line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM