I am writing a program (this has been days in the making) and I am a newb. We are learning if statements. I need to have the program allow a user to input their vehicle type (c,t,s) car, truck or senior citizen. I need the program to recognize the type of vehicle by one character and the number of hours parked, and then based on the type of car and the amount of time spent in the parking garage, charge the customer accordingly.
My problem:
I am not sure how to nest them, as every time I try, I break it. I have looked in the book but honestly just get very confused. Can someone give me the format for this?
The output of the program will accept the character and minutes parked, but also does both computations for car and truck. I need to do only do one set of calculations for that vehicle type.
Nesting them is actually very simple. Just copy and paste what you have inside of the { } of the appropriate if statement. Try it, and if you have a problem post it.
OK - I re-did the code to look cleaner, and in addition, I discovered a couple of things after I tested it several times again. First, it looks like the program is working for the "S"/senior type. Second, it also appears to be working for the "C"/car type. However, it appears to NOT be working for the "T"/truck type. I have looked at the statements handling the data and for the life of me cannot see why it is messing up only for that input.
Here is the corrected code:
removed
Any idea why it is messing up on just that input type?
Why not make all the conditions equalities instead of double inequalities. I would also chang cTotal and tTotal to total and cFree and tFree to free. You should also change else to else if and put another else for occasions when the wrong character is input. Don't just take my word for it though. This is your program and these are only suggestions.
Grex2595, thanks for the suggestions but my instructor didn't make that part of the assignment, and in all honesty, I am just taking things one step at time. I need to get the basics first and then tweak details. Thanks again though.
ccsdude,
Here are the corrections I made. I took your suggestion regarding
else if ( userveh == "T")
. It now works - thank GAWD. However, I am going to leave this thread unsolved just yet, because I have one more tweak to make. I need to allow for the user to enter minutes, not hours. Bottom line, I have some conversion to do and will have to adjust my statements accordingly. I may post back if I get hung up in that. Best part? After this, I get to try my luck with switch statements writing the same program. Please tell me this stuff gets a little easier as experience builds. Thanks for all your help. Sometimes the book and instructors only do so much. Thanks again.
For minutes to hours, you will need to divide by 60. Make the division integer division so it truncates the number of hours to a whole number, then use a modulus to get the remaining number of minutes.
128 / 60 = 2
128 % 60 = 8
A modulus gives you the remainder of division. 120 / 60 = 2 remainder 8.