C scanf in loop continues automaticly without inpu

2019-08-09 05:21发布

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.

2条回答
孤傲高冷的网名
2楼-- · 2019-08-09 05:56

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!

查看更多
爷的心禁止访问
3楼-- · 2019-08-09 05:57

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)

2)

    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
    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);
        for(cj = 0; cj < k; cj++){

            if(scanf("%i", &temp[cj]) != EOF)
            {
               deeln[ci][cj] = temp[cj]-'0'; 
            }
            else deeln[ci][cj] = -1;
        }
    }
    getchar();

    //loop while. 
}

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
    scanf("%i", &k);
    scanf("%i", &d);
    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
}
查看更多
登录 后发表回答