【发布时间】: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 提供更多输入之前。
【问题讨论】:
-
%s和int数组??? -
景色不错,会试试看!
-
也初始化变量。
-
但是不行,不行,我还是不能真正输入所有内容,当我在开头输入2和5时,我只能输入3次。
-
您应该经常做的一件事是检查
scanf的返回值。否则,您将最终调试(非)错误,这些错误只是由您未处理的意外无效输入引起的。