if else statement trouble

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.

Here is my code:
removed
Last edited on
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 - here is the code after I copied and pasted:

removed

Here is the output:

v245-2% ./a.out
Please enter your vehicle type with characters C, T, or S:
T
Please enter hours parked:
210
You owe: 52
You owe: 52
You owe: 156.75


It seems to be doing all the calculations again.
Last edited on
What you should do is put the code that charges inside the if statement that sees what kind of car it is, I think it would be something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
else if ( userveh != 'S' && userveh !='C') {		 
	cout << "You are a truck." << endl;

        // handle charges for truck
        tfree = hours - 1;
        if (tfree > 1 && tfree < 4) {
	tTotal = tfree * 1.00;
	cout << "You owe: " << tTotal << endl;
        }
        else if (tfree > 4) {
	tTotal = tfree * .75;
	cout << "You owe: " << tTotal << endl;
        }
        else
	cout << "You owe nothing." << endl;
}


P.S: I am not in my computer so I can't test it
P.S.S: I just copy&paste your code so syntax is probably wrong
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?

Thanks,
A
Last edited on
Try changing the condition to else if ( userveh == "T") if that doesn't work I will try to run it on my computer and see
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.

Code that works: YAY!

removed
Last edited on
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.
GRex2595, thanks but I had figured it out so I marked the thread solved. Thanks again.
Topic archived. No new replies allowed.