-
Coding Problem-Help plz
HI, I'm new at this so I just need a little bit of assistance in this. I have done the source code and everything for this problem but can't do 1 thing. I need the program to end when I type 99 hours. Below is the problem and source code. I would appreciate anyone's help. thanks.
A parking garage charges a $2.00 minimum fee to park for up to 3 hours. The garage charges an additional $0.50 per hour for each hour or part thereof in excess of 3 hours. The maximum charge is $10.00. (Hint: That means you dont have to calculate for anything over 18 hours.) Write a program that calculates and prints the parking charges for a number of cars. The user will enter the number of hours in hours and tenths of an hour. For example, the user will enter 4.5 for 4 hours and 30 minutes. The user will enter 99 hours to signal that the program should end.
Code:
#include <stdio.h>
int main()
{
int car[3];
double hours[3], total_hours = 0;
double charge[3], total_charge = 0;
int i;
for ( i = 0; i < 3; ++i )
//car[i]=0;
{
printf("Enter car number? ");
scanf("%d", &car[i]);
printf("Enter hours parked? ");
scanf("%lf", &hours[i]);
if ( hours[i] < 3 )
{
charge[i] = 2.0;
}
else if ( hours[i] < 18 )
{
charge[i] = 2.0 + (hours[i] - 3.0) * 0.50;
}
else
{
charge[i] = 10.0;
}
//printf("%1f%1f\n", "car" "hours" "charge");
//for (car=1; car < 3; ++car) {
//printf("%d%1f%1f\n", car,hours,charge[hours]);
//printf("%.2f%.2f\n", total_hours[hours],total_charge[charge]);
total_hours = total_hours + hours[i];
total_charge = total_charge + charge[i];
}
printf("%s %s %s\n", "car", "hours", "charge");
for (i=0; i < 3; ++i) {
printf("%d %.1f %.2f\n", car[i], hours[i], charge[i]);
}
printf(" %.2f %.2f\n", total_hours/*[hours]*/,total_charge/*[charge]*/);
return 0;
}
-
Great, so you've posted some source code. What's the problem? What does it do? What doesn't it do that it should? What does it do that ti shouldn't? What output does it generate now? What output is it supposed to output? Starting to understand now? In other words: Ask Smart Questions!
Quzah.
-
end the program
I need the program to end when I type 99 hours...I dont know how to do that!
-
break will break out of a loop.
-
Like
Code:
int i = 0;
while(1) {
scanf("%i", &i);
if(i==99) break;
printf("%i", i);
}
printf("\ndone\n");
Input/output:
Code:
2
3
7
85
200
99
done
Or you can put another condition in the for loop:
Code:
for ( i = 0; i < 3 && input != 99; ++i )
-
-
oh sorry, one more thing, how do I put this into my code?
-
In here
Code:
printf("Enter hours parked? ");
scanf("%lf", &hours[i]);
/* . . . right here . . . */
if ( hours[i] < 3 )
{
charge[i] = 2.0;
}
Add something like
Code:
if(hours[i] == 99) break;
That will break out of the
Code:
for ( i = 0; i < 3; ++i )
//car[i]=0;
{
loop.
-