好的,所以我在管理数组/ for循环时遇到了麻烦(对于C来说还很新)。我需要问用户要输入多少种涂料,然后对每种涂料取三遍数据,最后输出所有数据。
我似乎可以从用户那里获取所有数据,这主要是在我苦苦挣扎的最后输出所有数据。我不是在寻找针对此特定问题的快速解决方案,因为我想了解输出数据时数组/ for循环的工作方式(如果有任何意义)。
#include <stdio.h>
int main(void)
{
int amount, count;
int result1, result2, result3;
char paintname;
printf("Please enter how many paints you want to compare:\n");
scanf("%d", &amount);
for (count = 1; count <= amount; count++)
{
printf("Please enter the name of paint number %d:\n", count);
scanf("%s", &paintname);
printf("Please enter the first result of paint number %d:\n", count);
scanf("%d", &result1);
printf("Please enter the second result of paint number %d:\n", count);
scanf("%d", &result2);
printf("Please enter the third result of paint number %d:\n", count);
scanf("%d", &result3);
}
return 0;
}
最佳答案
如果要查找所有结果的存储方式,则应为每个结果(和名称)使用一个足够大以容纳所有用户输入的数组。该数组的大小是动态的(即,它是在用户输入时在运行时确定的),因此应先使用malloc()
/ calloc()
然后使用free()
对其进行动态分配。稍后的。
关于c - 最后需要接受用户输入并输出所有内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33404149/