我正在尝试编写一个使用 scanf()
接收多个值的代码。取决于之前的输入是什么。例如
printf("enter how many trips you are taking:");
scanf("%f", &trip)
printf("please enter the cost of each trip:");
^--我无法弄清楚如何在不限制成本输入值数量的情况下扫描多个输入值
最佳答案
显而易见的答案是循环读取值:
float *trip_costs;
trip_costs = malloc(trip * sizeof(*trip_costs));
for (i=0; i<trip; i++)
scanf("%f", trip_costs + i);
另请注意,由于您将其用于计数,因此 trip
作为整数类型可能会更好。
关于c - 如何从前一个变量中获取多个 scanf 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4857482/