/*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 (c开发者_如何学运维ustomer < 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;
}
I'm having issues with the inputs, I'm trying to make the loop run as the user request another calculations. But when ever it re runs another test it keeps the input of the previous test is there a way to reset the variables to 0? Ive tried assign all of the variable inside and outside of the loop but I'm kinda lost on what to go from here.
Move the declaration of the variables to sit inside the loop.
while ( response != 0 )
{
float customer = 0, stops = 0, response = 0, cost = 0, i = 0, stop = 0, total = 0, halfcost = 0, totaltrip = 0;
...
}
精彩评论