0

I am trying to input some integers into a 2 dimensional array. I am using a for loop in a for loop for iterate through the array. One problem i am facing is that lets say the 2d array is v[3][3], when the user enters 4 digits in one line for when y = 0, i want to give out an error. I've tried using counters to see if the user has inputted more than the desired amount, but instead i just goes to the second line of array and inputs the fourth integer into v[0][1]. Thank you.

This is what i've tried, the logic of "if(x==i)" is wrong, and i'm not sure how to fix this.

int main(void) {

    // TODO


    int x = -1;
    int y = -1;
    scanf("%d %d", &x, &y);

    if (x <= 0 || y <= 0) {
        printf("Cannot decode\n");
        return 1;
    }

    int v[x][y];

    for (int t=0; t < y; t++) {
        for(int i=0; i < x; i++) {
            // printf("i = %d\nt = %d\n", i, t);

            if (x == i) {
            printf("Cannot decode\n");
            return 1;
            }

            scanf("%d", &v[i][t]);

            // printf("%d-%d\n", i, t);

        }
    }
}

When i try to use fgets, i keep getting the errors:

nonogram.c:39:11: error: incompatible integer to pointer conversion 
passing 'char' to parameter of type 'char *'; take the address with & 
[-Werror,-Wint-conversion]
fgets(temp, x, stdin);
^~~~
&
/usr/include/stdio.h:564:38: note: passing argument to parameter '__s' 
here
extern char *fgets (char *__restrict __s, int __n, FILE 
*__restrict__stream)
^
1 error generated.

This is what i did

        for (int t=0; t < y; t++) {
            for(int i=0; i < x; i++) {

                fgets(temp, x, stdin);
                v[i][t] = atoi(temp);

            }
        }
FatimahFatCakes
  • 331
  • 1
  • 3
  • 11
  • 3
    If you need to know that lines are formatted correctly, you have to read lines ([`fgets()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html) or perhaps POSIX [`getline()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/getline.html)) and then process the line with [`sscanf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html) — possibly [using `sscanf()` in a loop](https://stackoverflow.com/questions/3975236/how-to-use-sscanf-in-loops). Plain `scanf()` and friends do not care about lines; newlines are white space and are mostly ignored. – Jonathan Leffler Apr 12 '18 at 04:42
  • Cheers! i'm still getting to grips with fgets and will try to get it working in my program – FatimahFatCakes Apr 12 '18 at 05:09
  • when calling any of the `scanf()` family of functions, always check the returned value, not the parameter values, to assure the operation was successful. – user3629249 Apr 12 '18 at 05:33
  • this statement: `if (x == i) {` will never be `true` so the resulting code block will never be executed – user3629249 Apr 12 '18 at 05:42
  • generally, when designing an array with multiple indexes, the meaning of each index is: array[ row ][ column ] So the posted code is reading a column of data, then stepping to the next column. Probably not what you want to do. – user3629249 Apr 12 '18 at 05:48
  • 1
    when a complete (in this case) column of numbers has been entered, you could then clear 'stdin' by calling `getchar()` until the read char is either EOF or '\n' – user3629249 Apr 12 '18 at 05:51

1 Answers1

0

if i'm not wrong you want to give error for all y=0 indexes as per your Words

when the user enters 4 digits in one line for when y = 0, i want to give out an error

to give an error in your loop for all y=0 indexes i.e v[][0] you must write the if statement like this:

if(i==0){
print("");
return 1;
}
else{
scanf();
}

Definitely x can never equal to i because in your example your are saying it is 3 and in your code you are saying that if 3==0 then producing your error message, it will never execute. it will always skip your if statement because it is becoming false each time and will execute scanf() statement.

Note* if you don't want to put constant in your iff statement then you can use variable and can declare it as constant.

  • if the condition is (i == 0), the for loop will never run since i starts at 0. – FatimahFatCakes Apr 12 '18 at 05:08
  • definitely it will execute use variable instead of 0 and make it constant , try this and then comment below, one more thing you must declare a garbage variable before return statement which will keep track that all the y's are executed or not if all the y's executed then execute your return statement. – Mustajeeb ur Rehman Apr 12 '18 at 05:18