【发布时间】:2021-05-21 19:38:19
【问题描述】:
下面的程序要求用户输入 10 个整数并将用户输入存储在相应的 10 个整数数组中。当输入完成时,它应该遍历已经存在的用户输入并将它们打印出来。我想通过使用像strlen() 这样的函数来遍历storedinputs 中的存储值,但是存储的值不是字符串而是整数。我怎么会做这种事。
int main(void) {
int storedinputs[10] = {0};
int input;
for(int i = 0; i < 10; i++) {
printf("\nPlayer input:");
scanf("%d", &input);
storedinputs[i] = input;
for(int s = 0; s < strlen(storedinputs); s++) {
printf("Inputs %d", storedinputs);
}
}
return 0;
}
预期输出:
Player input: 1
Inputs: 1
Player input: 3
Inputs: 1 3
Player input: 60
Inputs: 1 3 60
【问题讨论】:
-
对于每个
i,您都有准确的i+1存储输入。 -
你不能申请
strlen(storedinputs)。您传递了错误的类型。内循环可以是for(int s = 0; s <= i; s++)
标签: c function for-loop input user-input