Please help. When I compile this program, I don’t get any errors, but when I run
ID: 3575495 • Letter: P
Question
Please help. When I compile this program, I don’t get any errors, but when I run it, it crashes. I don’t know what’s wrong or how to fix it. Also, do I need a condition that checks for less than 1 passenger?
The code is in response to this scenario:
Write a program that will calculate the fare for a cab ride based on the rates provided below. Ask the user to input distance (measured to 1/10 of mile), number of passengers, and if going to/coming from the airport. Calculate fare for trip using these rates:
· Minimum fare: $5.00 (fares less than this rounded up to $3.50).
· $1.80 per mile (charged by 1/10 of mile. 2.1 miles = $3.78).
· 1st additional passenger, if more than 1 rider: $1.00.
· Each additional passenger (after 1st additional passenger): $0.50.
· Airport surcharge: $2.00.
#include <stdio.h>
int main()
{
int distance, passenger, airport;
float fare;
printf("Enter distance traveled");
scanf("%d", distance);
printf("Enter number of passenger ");
scanf("%d", passenger );
printf("Going one if going to/for airport ");
scanf("%d", airport);
if (airport == 1)
{fare= (distance*1.80) + 2;}
else {fare = distance*1.80;};
if (passenger <=1)
{fare=fare+1;}
else if (passenger>1)
{fare = fare + 1 + (0.50* (passenger-1));}
printf("Your fare cost");
{
return 0;
}
}
Explanation / Answer
Hi
I have fixed the issues and highlighted the code cchanges below
#include <stdio.h>
int main()
{
int distance, passenger, airport;
float fare;
printf("Enter distance traveled");
scanf("%d", &distance);
printf("Enter number of passenger ");
scanf("%d", &passenger );
printf("Going one if going to/for airport ");
scanf("%d", &airport);
if (airport == 1)
{fare= (distance*1.80) + 2;}
else {fare = distance*1.80;};
if (passenger <=1)
{fare=fare+1;}
else if (passenger>1)
{fare = fare + 1 + (0.50* (passenger-1));
}
printf("Your fare cost: %f ", fare);
return 0;
}
Output:
sh-4.2$ gcc -o main *.c
sh-4.2$ main
Enter distance traveled2
Enter number of passenger 3
Going one if going to/for airport 4
Your fare cost: 5.600000