我正在尝试在数组中获取输入,我希望输入如下。
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("cj is nu %i n", cj);
之后输入printf,那么输出也刚好在循环按自己的方式进行之后。在此之前,我应该使用第三个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)
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, check values before allow code to continue
if(scanf("%i", &k) != 1) {printf("scanf failed");return 0;}
if(scanf("%i", &d) != 1) {printf("scanf failed");return 0;}
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();
return 0;// main returns int, use return statement
}
您可以使用temp[cj]的索引,使其成为您实际想要的索引,但我假设您打算从stdin读取,然后为每个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, check values before allow code to continue
if(scanf("%i", &k) != 1) {printf("scanf failed");return 0;}
if(scanf("%i", &d) != 1) {printf("scanf failed");return 0;}
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
return 0; // main returns int, use return statement
}