Just wondered if this will put the titles in tabular format:
printf("%13s%13s%13s\n", "Car", "Hours", "Charge");
if not, what's wrong with it?
thanks,
Extro
Just wondered if this will put the titles in tabular format:
printf("%13s%13s%13s\n", "Car", "Hours", "Charge");
if not, what's wrong with it?
thanks,
Extro
Why don't you tell us if it does what you want or not and if not what does it not do that you want it to. We arn't compilers for you personnel use.
>Just wondered if this will put the titles in tabular format
Why not test it and see? Yes, it will, to a point.
My best code is written with the delete key.
Comments like thisdont help me, note the rules:Originally Posted by orbitz
"Keep in mind that being helpful does not give license to be rude."
I normally wouldn't care but I get this all the time...
To the people interested in helping here's my code if it helps understand what I'm trying to accomplish:
I just ran it again and it print the first statement but not the second, no idea why.Code:#include <stdio.h> float calculateCharges (int hours, float charge); int main() { int car =0; int hours; float charge; while (car <= 3){ printf("How many hours was the car parked for?: "); scanf("%d", &hours); printf("%s%13s%13s\n", "Car", "Hours", "Charge"); ++car; if(hours < 3){ charge = 2; printf("%d%13d%13f\n", car, hours, charge); } if(hours > 3){ printf("%d%13%13f\n" , car, calculateCharges(hours, charge)); } if(hours >= 24){ charge = 10; printf("%d%13d%13f\n", car, hours, charge); } } return 0; } float calculateCharges (int hours, float charge) { return charge = 2 + ((float)hours * 1 /2 ); }
Any help is appreciated,
Extro
Last edited by Extropian; 08-18-2005 at 06:33 PM.
Is this kind of what you are looking for?
The output:Code:#include <stdio.h> int main() { int hours[3]; ////////// // Get user input /////////// for(int car = 0; car < 3; car++) { printf("How many hours was the car[%d] parked for?: ", car); scanf("%d", &hours[car]); } ///////// // Print out stats ////////// printf("%-13s%-13s%-13s\n", "Car", "Hours", "Charge"); for(car = 0; car < 3; car++) { if(hours[car] <= 3) printf("%-13d%-13d%-13f\n", car, hours[car], 2.0); else if(hours[car] > 3) printf("%-13d%-13d%-13f\n", car, hours[car], 2 + (hours[car] * .5)); else if(hours[car] >= 24) printf("%-13d%-13d%-13f\n", car, hours[car], 10); else printf("\n\n\twtf"); } return 0; }
Here's a nifty little format string (not the exploit...) reference: http://www.cplusplus.com/ref/cstdio/printf.htmlCode:How many hours was the car[0] parked for?: 20 How many hours was the car[1] parked for?: 5 How many hours was the car[2] parked for?: 2 Car Hours Charge 0 20 12.000000 1 5 4.500000 2 2 2.000000
>Comments like thisdont help me, note the rules:
Aside from being a tad rude, orbitz does make a good point. If you don't tell us what you expect, we can't tell you how to make the code do it.
>I normally wouldn't care but I get this all the time...
This is sign that you're asking the wrong questions, or you're asking questions that can be more easily answered through other means than posting a thread on a forum.
My best code is written with the delete key.
Thank you very much tonto I'm sure this will help, I'll compare your code to mine and see if I can fix it.
The only difference is it shoud only output:
Car Hours Charge
0 20 12.000000
1 5 4.500000
2 2 2.000000
I'm also supposed to use a user defined function (gotta show we know how to use them)
i"ll let ya know if I get it.
Thanks again,
bryan
I don't see a difference between the output you expected and the one I posted. Except mine had questions. I mean, you do want the user to enter the number of hours and such right? I just copied the entire programs run history in the console to here, so the questions aren't technically part of the feedback of your program.
On another note, it is important to ask more specific questions than:
This makes the reader think that you made no attempt to try for yourself, in addition to the fact that it is extremely vague and doesn't make much sense. Anyways, uh, good luck on your assignment, and take into consideration what the others have said here. Make your questions clear, concise, thought through, and whatever else makes a good question.Originally Posted by You
You missed the rule that states:Originally Posted by Extropian
"Don't be such a lazy ........ing idiot. Try something for yourself once in a while you lazy ass!"
Then maybe you should take a hint and start:Originally Posted by Extropian
a) Asking smarter questions (link's already been provided for you).
b) Stop being so lazy, and try something yourself.
Why, since you are such a stickler for rules, here's another one, aside from the first one I mentioned, that you should read:Now you may say "well this isn't homework so it doesn't apply". Well it does apply. Obviously it applys a great deal, since you have stated:So take a hint, grab a clue, and try your call again.Originally Posted by Extropian
Quzah.
Hope is the first step on the road to disappointment.
I'm so close to being done with my code for this one but my function is only retuning 2.000. I really wrapped my brain around this one and can't seem to figure out why it won't work.
Any thoughts? I'm new to user defined functions (and I'm required to use one here)
Regards,
Extro
Code:#include <stdio.h> float calculateCharges(float charge); int hours; float charge; int main() { int hours[3]; for(int car = 0; car < 3; car++) { printf("How many hours was the car[%d] parked for?: ", car); scanf("%d", &hours[car]); } printf("%-13s%-13s%-13s\n", "Car", "Hours", "Charge"); for(car = 0; car < 3; car++) { if(hours[car] <= 3) { charge = 2.0; printf("%-13d%-13d%-13f\n", car, hours[car], charge); } if(hours[car] > 3) { printf("%-13d%-13d%-13f\n", car, hours[car], calculateCharges(charge)); } if(hours[car] >= 24) { printf("%-13d%-13d%-13f\n", car, hours[car], 10); } printf("\n\n"); } return 0; } float calculateCharges(float charge) { charge = 2 + ((float)hours * 1 / 2); return charge; }
I wouldn't recommend using global variables to help confuse yourself.
Shouldn't the function calculate a charge based on the number of hours? Perhaps like this?
Then your loop could be simplified.Code:float calculateCharges(int hours) { float charge = 2.0F; if ( hours > 24 ) { return 10; } else if ( hours > 3 ) { charge += hours / 2.0F; } return charge; }
Code:printf("%-13s%-13s%-13s\n", "Car", "Hours", "Charge"); for ( car = 0; car < 3; car++ ) { printf("%-13d%-13d%-13f\n", car, hours[car], calculateCharges(hours[car])); }
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*