I am writing a program that asks the user to input the amount of miles it will t
ID: 3543476 • Letter: I
Question
I am writing a program that asks the user to input the amount of miles it will take to reach a destination, the amount of miles it takes to stop for gas during the trip, and the amount of miles it would take for someone to stop at a restaurant along the trip to eat. The breaks are then printed in order, and if the miles get to be the same at some point for each break, the program will tell the user to stop for both types of break at the same mile number. Here is an example:
How many miles will it take to reach your destination?
> 150
How many miles will it take to stop in between for gas?
> 30
How many miles will it take to stop in between at a restaturant to eat?
>40
Output:
Stop after 30 miles for gas
Stop after 40 miles for food
Stop after 60 miles for gas
Stop after 80 miles for food
Stop for both at mile 120
My program is working for the most part, but I am doing something wrong when the miles are equal. I set conditions so that when gas and restaurant breaks aren't equal, they print separately, and when they are equal, they print under both. Please help! Thank you so much!
Explanation / Answer
#include<stdio.h>
int main()
{
int stop_for_gas;
int stop_for_eating;
int destination;
int start = 0;
int gas_stop;
int restaurant_stop;
printf(" How many miles will it take to reach your destination? ");
scanf("%d",&destination);
printf(" How many miles will it take to stop in between for gas? ");
scanf("%d",&stop_for_gas);
gas_stop = stop_for_gas;
printf(" How many miles will it take to stop in between at a restaturant to eat? ");
scanf("%d",&stop_for_eating);
restaurant_stop = stop_for_eating;
while(start <= destination)
{
if(gas_stop >= destination || restaurant_stop >= destination)
{
//printf(" You have reached Destination ");
break;
}
if(restaurant_stop%stop_for_gas==0)
printf(" Stop for both at mile %d",restaurant_stop);
else
{
printf(" Stop after %d miles for gas " , gas_stop);
printf(" Stop after %d miles for food ", restaurant_stop);
}
gas_stop = gas_stop + stop_for_gas;
restaurant_stop = restaurant_stop + stop_for_eating;
start = start + (gas_stop>restaurant_stop)?gas_stop:restaurant_stop;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.