在循环Çscanf函数全自动继续无需投入 [英] C scanf in loop continues automaticly without input

查看:103
本文介绍了在循环Çscanf函数全自动继续无需投入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让输入一个数组,我预计投入像下面这样。

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)

因此​​,我们得到deeln阵列[2] [5]在本实施例。我试着用下面的code获得它:

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

不过,我有一个问题,每当我试着输入,程序运行,全自动没有得到任何输入时,它绕一圈第三scanf函数的第二或第三的时间。于是我不能输入任何东西。

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.

该怎么办?有它是与指针或正在使用scanf函数错了我?

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

更新:

如果我输入后的printf一个printf(CJ是NU%I \\ N,CJ); 则输出也刚刚中,环走自己的后办法。而不是之前我应该​​给更多的投入,使用第三scanf函数。

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.

推荐答案

我的问题的解决方案是很容易的。我发现我输入的思前想后。的问题是,在输入,如上所述,有空间。不知怎的,scanf函数不能以空格处理,除非你使用一些其他的语法。但我的解决方法就是使用与fgets代替scanf函数的,我希望得到的投入。因此,新的和工作code是如下:

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!

这篇关于在循环Çscanf函数全自动继续无需投入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆