1

I'm trying to get input in an array, I expect input like the following.

5 (Number of the second dimensions in the array)
2 (Number of the first dimensions in the array)

So we get an array deeln[2][5] in this example. I try to get it with the following code:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isinarray(int val, int *arr, int size){
    int countimp;
    for (countimp=0; countimp < size; countimp++) {
        if (arr[countimp] == val)
            return true;
    }
    return false;
}


int main(void){

    int k, d, ci, cj, ck, ta;
    //get input
    scanf("%i", &k);
    scanf("%i", &d);

    int deeln[d][k], temp[k];

    for(ci = 0; ci < d; ci++){
        printf("d= %i, ci= %i \n", d, ci);
        scanf("%s", temp);
        for(cj = 0; cj < k; cj++){
            deeln[ci][cj] = temp[cj*2]-'0';
        }
    }

    //loop while. 
}

But i've got a problem, whenever i try to input, the program runs automaticly without getting any input when it loops around the third scanf for the 2nd or 3rd time. So then i'm not able to input anything.

What to do? Has it something to do with pointers or am i using scanf wrong?

UPDATE:

If I enter a printf after printf("cj is nu %i \n", cj); then the output also just came after the loop was going its own way. and not before i should give more input, using the third scanf.

ryyker
  • 22,849
  • 3
  • 43
  • 87
Genie Kort
  • 81
  • 11

2 Answers2

2

The solution of my question was quite easy. I found it after thinking of my input. The problem was that in the input, as described, there were spaces. Somehow scanf can't handle with spaces, unless you use some other syntax. But my solution is to just use fgets instead of scanf where I wanted to get the input. So the new and working code is as follows:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool isinarray(int val, int *arr, int size){
    int countimp = 0;
    for (countimp=0; countimp < size; countimp++) {
        if (arr[countimp] == val)
            return true;
    }
    return false;
}


int main(void){


    int t, k = 0, d = 0, ci = 0, cj = 0, ta = 0;


    //get input
    scanf("%i", &k);
    scanf("%i", &d);
    char temp[20];
    int deeln[d][k];

    memset(deeln, 0 , sizeof(deeln));
    memset(temp, 0 , sizeof(temp));




    for(ci = 0; ci < d; ci++){
        fgets(temp, 20, stdin);
        for(cj = 0; cj < k; cj++){
            ta = cj*2;
            deeln[ci][cj] = temp[ta]-'0';
        }
    }

    //loop while. 
    return 1;
}

Thanks for helping everbody, even though we all didn't came to this. But I hope it will help others!

Genie Kort
  • 81
  • 11
1

Two places to look:

1)

cj = 0;//initialize cj before using here
scanf("%i", &temp[cj]);//temp is both an array, and an int.  Fix your format specifier,
                       //and use an index operator - temp[?] (not sure I am using the right index)
  1.   deeln[ci][cj] = temp[cj*2]-'0'; //fix your logic here (array index will be exceeded)
    

An example of working code...

int main(void){

    int k, d, ci, cj, ck, ta;
    //get input, check values before allow code to continue
    if(scanf("%i", &k) != 1) {printf("scanf failed");return 0;} 
    if(scanf("%i", &d) != 1) {printf("scanf failed");return 0;} 

    int deeln[d][k], temp[k];

    for(ci = 0; ci < d; ci++){
        printf("d= %i, ci= %i \n", d, ci);
        for(cj = 0; cj < k; cj++){

            if(scanf("%i", &temp[cj]) != EOF)
            {
               deeln[ci][cj] = temp[cj]-'0'; 
            }
            else deeln[ci][cj] = -1;
        }
    }
    getchar();
    return 0;// main returns int, use  return statement
}

you can play with the index of temp[cj] to make it what you actually want, but I assume you are intending to read from stdin, then populate deeln[][] with that value, for each scanf.

If you want to parse a string containing spaces and digets, "1 3 8 5 3", you could use strtok() But your code as it is is not reading a string in, it is reading integers.

This is not perfect, you will have to do some debug, but will illustrate strtok(). You have to enter spaces between each digit after indices are selected: i.e.:

3
3
4 6 8
2 4 7
1 2 8  

int main(void){

int k, d, ci, cj, ck, ta;

//get input, check values before allow code to continue
if(scanf("%i", &k) != 1) {printf("scanf failed");return 0;} 
if(scanf("%i", &d) != 1) {printf("scanf failed");return 0;} 
char inStr[d][k*5]; //space for up to k 3 digit numbers with 1 space each
char *buf=0;

int deeln[d][k], temp[k];

for(ci = 0; ci < d; ci++){
    printf("d= %i, ci= %i \n", d, ci);
    if(scanf("%s ", inStr[ci]) != EOF)
    {
        buf = strtok(inStr[ci], " ");
        cj = 0;
        while(buf && (cj < k))
        {
            deeln[ci][cj] = atoi(buf); 
            cj++;
        }
    }
}
//getchar();waits for user input, pauses execution
return 0; // main returns int, use  return statement

}

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • About 1. I already fixed that scanf, to %s. And i do not have to index those arrays do i? – Genie Kort Oct 13 '14 at 18:24
  • See edit for fix that will allow code to run. Not sure it matches your requirements, but in short, you need to call `scanf` inside the inner loop – ryyker Oct 13 '14 at 18:25
  • but this code won't handle 5 numbers separated with spaces? – Genie Kort Oct 13 '14 at 18:27
  • Missed that in your description, No, it will not. But if you enter the first two values for your array order, say 3, 3, Then enter the following 9 requests for integers, it will populate your deeln array. – ryyker Oct 13 '14 at 18:29
  • I wouldn't say any code, which can easily lead to use of uninitialized variables, is "working code"... Please check return values of `scanf`, and only use the values parsed, if values were actually parsed successfully! – hyde Oct 13 '14 at 18:31
  • @GenieKort - to parse a string containing digits and spaces into an array of variables, you could use char *buf = strtok(buf, " "); See edit. – ryyker Oct 13 '14 at 18:40
  • Great thanks! But what I also would like to know is, how is it possible that scanf just doesn't work as expected? – Genie Kort Oct 13 '14 at 19:07
  • scanf is seeing white space. Hitting the return key actually results in a \n or \n\r (windows, linux) either of which makes scanf() difficult to use for entering and parsing strings. Look at ***[sscanf()](http://stackoverflow.com/q/9578611/645128)*** (***[Or here](http://www.cplusplus.com/reference/cstdio/sscanf/)***) as an alternate. Hope the example gets you in the right direction. – ryyker Oct 13 '14 at 19:16