/*Taking input from user to calculate the cost of a travel plan
using the number of travelers/ number of stops/ cost of each stop
giving both the half cost of a trip and its full price*/
int calculate( int total);
#include <stdio.h>
int main(void)
{
float customer, stops, response, cost, i, stop, total, halfcost, totaltrip;
i=0;
printf("Welcome to Airline express.\n");
printf("Would you like to calculate the cost of a trip?");
scanf("%f", &response);
while ( response != 0 )
{
printf("Please enter how many customer will be traveling. Children under 12 are counted as .5:");
scanf("%f", &customer);
if (customer < 0)
customer = 3;
printf("Please enter how many stops there are:");
scanf("%f", &stop);
while (i<stop)
{
printf("Please enter cost of the next stop:");
scanf("%f", &cost);
if (cost < 0)
cost = 100;
total +=cost;
i++;
}
halfcost = calculate(total);
printf("Half cost is: %f", halfcost);
totaltrip = total * customer;
printf("Total cost for trip is: %f\n", totaltrip);
printf("Would you like to calculate another trip?");
scanf("%f", &response);
}
return 0;
}
int calculate( int total)
{
return total/2;
}
我遇到了输入问题,我正试图在用户请求另一次计算时使循环运行。但是,当它重新运行另一个测试时,它保持前一个测试的输入是否有办法将变量重置为0? 我已经尝试在循环的内部和外部分配所有变量,但我有点迷失在这里要做什么。
答案 0 :(得分:2)
将变量的声明移动到循环内。
while ( response != 0 )
{
float customer = 0, stops = 0, response = 0, cost = 0, i = 0, stop = 0, total = 0, halfcost = 0, totaltrip = 0;
...
}