0

I am trying to save some ints into an array that are read from the user input . I don't know the number of ints on each line, only the number of lines which will also be the maximum number of ints on that line. For example if this is 5 then the user should input 5 lines with ints 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
0

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

What I know about scanf is that if it succeeds in reading the number and stores it in a variable then it should return 1. That is my first condition. If not then I supposed that it is not a number so my second if should take care of that. If i have on a line 4 5\n in my mind is that first it reads 4 stores it in the array then continues, it sees the white space and do nothing as any of my conditions take care of that, sees 5 and does the same thing as before, and then it sees \n which should be caught with getchar().
Yes.You are right; my bad. In my problem I had a variable that should be read from the keyboard and i hardcoded n=5 . It is the number of lines that the program should read and also the maximum number of int that are on a line
@AndreiMădălinOancă.: Check the answer
EDIT:Solved it afterwards.My bad for not giving enough space to the array
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.