I am not exactly sure what is going on here. The code seems to function the way I expect it to except that when I exit the while loop it repeats the math of the last run through of the loop. Sorry if this is a stupid question because I am not exactly sure what is happening inside the conditional parenthesis next to the "while".
Code:
#include <stdio.h>
int main (void)
{
char designator;
int arrive, depart, cost_for_this_auto, total_seniors, total_cars, total_trucks, number_of_hours, total_hours_all_autos, total_profit_all_autos=0;
int x;
printf("Welcome to XYZ parking! Please hit return to enter the parking lot.\n");
while((x=getchar())!=EOF)
//while(scanf("%c %d %d",designator,arrive,depart)!=EOF)
{
printf("If you are a senior enter S, otherwise if you have a car enter C or if you have a truck enter T:\n");
scanf("%c", &designator);
printf("Please enter arrival time in military format 0600-2200\n");
scanf("%d", &arrive);
printf("Please enter departure time in military format 0600-2200\n");
scanf("%d", &depart);
number_of_hours = ((depart-arrive)/100);
if (designator == 's')
{
total_seniors++;
cost_for_this_auto=0;
}
else if (designator == 'c')
{
total_cars++;
if (number_of_hours <= 2)
cost_for_this_auto = 0;
else if (number_of_hours <= 5)
cost_for_this_auto = (number_of_hours-2)*.5;
else
cost_for_this_auto = ((number_of_hours-5)*.25)+1.50;
}
else if (designator == 't')
{
total_trucks++;
if (number_of_hours <= 1)
cost_for_this_auto = 0;
else if (number_of_hours <= 3)
cost_for_this_auto = number_of_hours-1;
else
cost_for_this_auto = ((number_of_hours-3)*.75)+2;
}
total_profit_all_autos=total_profit_all_autos+cost_for_this_auto;
printf("you arrived at %d and departed at %d.\n Your total cost for parking today was $%d for %d hours of parking.\n Thank you come again!\n\n", arrive, depart, cost_for_this_auto, number_of_hours);
total_hours_all_autos+=number_of_hours;
}
printf("total hours all cars were parked=%d \n total seniors=%d \n total cars=%d \n total trucks=%d \n total profit=$%d", total_hours_all_autos, total_seniors, total_cars, total_trucks, total_profit_all_autos);
return 0;
}