![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 10
Rep Power: 0
![]() |
stuck at this program
This is the program
A parking garage charges $2 for minimum fee to park up to 3 hours and then a $.50 per hour charge after the 3rd hour. The maximum charge for any 24 hour parking is $10. Assume no car parks longer than 24 hrs, write a program that calculates and prints the parking charge for each of 3 customer. Result should be printed in tabular format. Hint: use function called calculateCHarge to determine charge for each customer
#include <stdio.h>
minfee = 2;
int main()
{
int car1,car2,car3,hour,hour2,hour3,charge1,charge2,charge3,
totalhour,totalcharge,minfee;
printf("Enter car number\n");
scanf("%d", &car1);
printf("How many hour did the car park\n");
scanf("%d", &hour);
if (hour>=3) {
charge1 = (minfee+hour*.50); }
else {
charge1 = 2.00; }
printf("Enter car number\n");
scanf("%d", &car2);
printf("How many hour did the car park\n");
scanf("%d", &hour2);
if (hour>=3) {
charge2 = (minfee+hour*.50); }
else {
charge2 = 2.00; }
printf("Enter car number\n");
scanf("%d", &car3);
printf("How many hour did the car park\n");
scanf("%d", &hour3);
if (hour>=3) {
charge3 = (minfee+hour*.50); }
else {
charge3 = 2.00; }
totalhour=(hour+hour2=hour3);
printf("%d", totalhour);
totalcharge=(charge1+charge2+charge3);
printf("%d", totalcharge);
return 0;
}Heres how output should be Car Hours Charge 1 1.5 2.00 2 4.0 2.50 3 24.0 10.00 Total 29.5 14.50 |
|
|
|
|
|
#2 |
|
Professional Programmer
|
looks like you got some debugging to do.
good luck with that dizz |
|
|
|
|
|
#3 |
|
Professional Programmer
|
I'll play around with it tonight and see if I can help out.
__________________
Amateurs built the ark Professionals built the Titanic |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
the logic is mostly correct you just have a lot of bugs.
at first I thought dizz was being pompus (sorry dizz), but these are the types of bugs you need to learn to spot and fix easily, and the best way to do that is to pay attention to compiler errors and debug your own code. I went ahead and listed them out anyway... couldn't help myself -global minfee has no datatype, or local minfee not initalized (you pick )-ch arge3 -totalhour=(hour+hour2 = hour3); -you need to use float instead of int for hours and charges -you always use if(hour>=3) instead of appropriate hour, hour2, hour3 -you never print out the car specific info.. only the totals half of these the compiler tells you about..... by the way the instructions say to call a function to calculate the charge Last edited by spydoor; Mar 18th, 2005 at 3:53 PM. |
|
|
|
|
|
#5 |
|
Professional Programmer
|
well, i'd appreciate a 'please' or a 'if you have time' once in a while, posting a homework question and expecting results is not a way to get around, a little respect is all i ask for.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
No, I agree that's why I wrote at first.
Your response seemd appropriate after I realized most of the errors were simple typos or using the wrong variable from copying and pasting. Looks like there was little effort by the original poster to debug the code. And like you said, this is clearly a homework problem which was posted without any specific question... guess that gets old quick.. I'm new to this board. But really I meant nothing against you.. I was just apologizing for jumping to conclusions about the intent of your post from my first read of it. Now I feel over apologetic anyway carry on..... |
|
|
|
|
|
#7 |
|
Professional Programmer
|
i understand, just letting people know my stance, there's a bunch of posts by me that aren't as nice..
no offence taken, no worries |
|
|
|
|
|
#8 |
|
Newbie
Join Date: Jan 2005
Posts: 20
Rep Power: 0
![]() |
That's something you can turn off.
Goto Project Settings (Alt+F7) then select the C/C++ tab. Select Category: Precompiled Headers Now select: Not using precompiled headers |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Mar 2005
Posts: 10
Rep Power: 0
![]() |
is there a way to calculate all the charges regardless of hours in 1 equation?
or i have to do if and else statements like wat i did above on the post. |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Mar 2005
Posts: 10
Rep Power: 0
![]() |
revised program
#include <stdio.h>
int main()
{
int car,counter=0;
float minfee=2.00,maxfee=10.00,hours,totcharge,tothours,
calculateCharge;
totcharge=0;
tothours=0;
counter=1;
while(counter<=3){
printf("enter car number\n");
scanf("%d",&car);
printf("enter hours parked\n");
scanf("%f",&hours);
if(hours<4) {
calculateCharge=minfee;
printf("%.2f",calculateCharge);
}
if(hours<=24) {
calculateCharge=(minfee+hours/2);
printf("%.2f",calculateCharge);
}
else {
calculateCharge=maxfee;
printf("%.2f",calculateCharge);
}
counter=counter+1;
}
while(hours>=3) {
calculateCharge=(minfee+hours/2);
tothours=tothours+hours;
printf("%.2f",tothours);
totcharge=totcharge+calculateCharge;
printf("%.2f",totcharge);
return 0;
}I dont know whats wrong with my program, it doesn't print out the tothours and totcharge. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|