簡體   English   中英

循環中的C scanf在沒有輸入的情況下自動繼續

[英]C scanf in loop continues automaticly without input

我正在嘗試在數組中輸入,我希望輸入如下。

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

所以我們在這個例子中得到了一個數組deeln [2] [5]。 我嘗試使用以下代碼獲取它:

#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. 
}

但是我遇到了一個問題,每當我嘗試輸入時,程序會在第二次或第三次循環第三次掃描時自動運行而不會得到任何輸入。 那么我就無法輸入任何東西了。

該怎么辦? 它與指針有關或我使用scanf錯了嗎?

更新:

如果我在printf之后輸入printf("cj is nu %i \\n", cj); 然后輸出也是在循環以自己的方式進行之后。 而不是在我應該提供更多輸入之前,使用第三個scanf。

我的問題的解決方案非常簡單。 在考慮了我的輸入后我發現了它。 問題是在輸入中,如上所述,有空格。 不知何故,scanf無法處理空格,除非您使用其他語法。 但我的解決方案是只使用fgets而不是scanf,我想獲得輸入。 所以新的和工作的代碼如下:

#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;
}

感謝幫助任何人,即使我們都沒有來到這里。 但我希望它會幫助別人!

兩個地方看:

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)

工作代碼的一個例子......

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. 
}

你可以使用temp [cj]的索引來使它成為你真正想要的,但我假設你打算從stdin中讀取,然后用每個scanf的deeln [] []填充該值。

如果你想解析一個包含空格和digets的字符串,“1 3 8 5 3”,你可以使用strtok()但是你的代碼不是讀取字符串,而是讀取整數。

這不是完美的,你將不得不做一些調試,但將說明strtok() 在選擇索引后,您必須在每個數字之間輸入空格:即:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM