【问题标题】:C scanf in loop continues automaticly without inputC scanf in loop 无需输入自动继续
【发布时间】:2014-10-13 17:50:45
【问题描述】:

我正在尝试获取数组中的输入,我希望输入如下所示。

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 循环时自动运行而没有任何输入。所以我无法输入任何东西。

怎么办?它与指针有关还是我使用 scanf 错误?

更新:

如果我在printf("cj is nu %i \n", cj); 之后输入 printf,那么输出也只是在循环按照自己的方式进行之后出现。而不是在我应该使用第三个 scanf 提供更多输入之前。

【问题讨论】:

  • %sint 数组???
  • 景色不错,会试试看!
  • 也初始化变量。
  • 但是不行,不行,我还是不能真正输入所有内容,当我在开头输入2和5时,我只能输入3次。
  • 您应该经常做的一件事是检查scanf 的返回值。否则,您将最终调试(非)错误,这些错误只是由您未处理的意外无效输入引起的。

标签: c arrays scanf continue


【解决方案1】:

我的问题很容易解决。在考虑了我的意见后,我找到了它。问题是在输入中,如上所述,有空格。不知何故 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;
}

感谢大家的帮助,尽管我们都没有来这里。但我希望它会帮助其他人!

【讨论】:

  • 如果这对你有用,请将其标记为完成(上面的空心复选标记)顺便问一下,isinarray(...) 是做什么用的?
  • 我不能这样做,因为这是我自己的答案。我得等两天。
  • 哦 - 一个代表的东西。好吧,+1 表示坚持并找到fgets() 方法。这是一个真正的改进。
  • 而不是fgets(temp, 20, stdin);,考虑fgets(temp, sizeof temp, stdin);。更容易维护代码并且更有可能被正确编写。
【解决方案2】:

两个地方看:

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] 的索引来使其成为您真正想要的,但我假设您打算从标准输入读取,然后为每个 scanf 填充该值的 deeln[][]。

如果你想解析一个包含空格和数字的字符串,“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
}

【讨论】:

  • 关于 1. 我已经将 scanf 修复为 %s。而且我不必索引这些数组吗?
  • 查看编辑以获取允许代码运行的修复。不确定它是否符合您的要求,但简而言之,您需要在内循环中调用scanf
  • 但是这段代码不能处理用空格分隔的 5 个数字?
  • 在您的描述中错过了这一点,不,不会。但是,如果您输入数组顺序的前两个值,例如 3、3,然后输入以下 9 个整数请求,它将填充您的 deeln 数组。
  • 我不会说任何容易导致使用未初始化变量的代码是“工作代码”......请检查scanf的返回值,并且只使用解析的值,如果值实际解析成功!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-13
  • 1970-01-01
  • 2017-06-15
  • 2023-03-10
  • 2014-06-13
  • 1970-01-01
相关资源
最近更新 更多