Hey everyone!

I'm trying to figure out the last step in completing this application. It compiles fine, I just can't seem to get the right answers! It's something to do with differentiating the two switch statements when calculating the results...between "car_type" and "ins_option".

I'm a beginner so I'm not too familiar with methods.

//Calculate totals and display results.

        txtRentalTotal.Text = bill(car_type, num_rental_days, num_periods).ToString("C");
        txtInsuranceTotal.Text = bill(ins_option, num_ins_days, num_periods).ToString("C");
        txtFinalTotal.Text = (bill(car_type, num_rental_days, num_periods) + bill(ins_option,  num_ins_days, num_periods)).ToString("C");

    
private double bill (char type, int days, int periods)
    {
        // Daily Rental Prices.
        const double compact_price = 34.95;
        const double full_price = 49.95;
        const double sport_price = 75.95;

        // Daily Insurance Prices.
        const double collision_ins = 18.49;
        const double full_ins = 34.99;

            switch (car_type)
            {
                case 'C':
                    return compact_price * num_rental_days * num_periods;

                case 'F':
                    return full_price * num_rental_days * num_periods;

                case 'S':
                    return sport_price * num_rental_days * num_periods;
            }


            switch (ins_option)
            {
                case 'C':
                    return collision_ins * num_ins_days * num_periods;

                case 'F':
                    return full_ins * num_ins_days * num_periods;

                case 'N':
                    return 0;

                default:
                    messageBoxOK("Fatal error: The program should never get here!");
                    return 0;

            }
    }

Any help would be appreciated!

Recommended Answers

All 5 Replies

Im surrpised it doesnt whine about drop throughs .. as there are no breaks in your switch statements.

Ok, give us an example of the input values you have, and the output you expected, and what you actually got.

Im surrpised it doesnt whine about drop throughs .. as there are no breaks in your switch statements.

Ok, give us an example of the input values you have, and the output you expected, and what you actually got.

This is just a basic application for school. You enter letters and numbers as input that correspond to different car rental options. Kind of pointless... We're just covering methods now.

So there is a "car type" parameter and an "insurance option" that I need to use to calculate subtotals for car rental and insurance. I am supposed to do this in one method. So I pass "type" as a parameter thinking I can use a switch statement for each type.

Car type and Insurance Options both contain the case "C" and "F" and therefore I always get the total for "C" and "F" from the "car type" switch since it comes first in the code, I'm guessing.

So, I'm not really sure, but how can I separate the switch statements to recognize which "type" parameter (car_type or ins_option) is being passed through the method?

Oh yeah, I was going to use breaks in the switch statements, but it warns me that the code after it is unreachable.

it seems you couldnt answer the question.

Debug your code, watch where it goes, what what it does, it will help you find why its wrong

if entering breaks in your switch statements tell you code isnt reachable, then you've done something wrong.

Do remember, when you say "return" it will do just that, no code after is processed.. so, if you have to run both switch statements, thats your issue..

I guess I really don't understand what the deal is then. In my professor's sample code for a past assignment, he uses multiple return statements in one switch statement and it works fine.

private decimal bill(char customerType, int additionalPrograms, int installations)
    {
        const decimal HOME_BASE = 99M;
        const decimal HOME_PROGRAM = 49M;
        const decimal ENTERPRISE_BASE = 249M;
        const decimal ENTERPRISE_PROGRAM = 99M;
        const int ENTERPRISE_BASE_INSTALLATIONS = 10;
        const double ENTERPRISE_ADD_INSTALLATION = .1;
        const decimal UNLIMITED = 999M;

        // Calculate Bill
        switch (customerType)
        {
            case 'H':
                return HOME_BASE + HOME_PROGRAM * (decimal)additionalPrograms;
            case 'E':
                if (installations <= ENTERPRISE_BASE_INSTALLATIONS)
                {
                    return ENTERPRISE_BASE + ENTERPRISE_PROGRAM * (decimal)additionalPrograms;
                }
                else
                {
                    return (ENTERPRISE_BASE + ENTERPRISE_PROGRAM * (decimal)additionalPrograms)
                        * (1 + (decimal)(installations - ENTERPRISE_BASE_INSTALLATIONS) * (decimal)ENTERPRISE_ADD_INSTALLATION);
                }
            case 'U':
                return UNLIMITED;
            default:
                messageBoxOK("Fatal error: The program should never get here!");
                return 0M;
        }
    }

Should I post my entire code? Would that make this easier? When I debug, I don't get any errors besides wrong output. The input values "N" and "S" supply me with the correct input, but when I input "C" or "F" regardless if it is in the car_type textbox or the ins_option textbox, I always get output from the car_type switch statement.

Thats because he has 1 switch statement, you have 2.

As I said, when your code comes along a return statement no further code is processed

so, picture the following pseudo code.

x = 3
y = 4

if x < 5 return "small x"
if y < 5 return "small y"

so..
in this case, the second if never runs because you have already returned "small x"

you can only come out once for each time you go in.

Im guessing your thoughts for N+S you think its right because the value you assign is 0 to one of them.. eg the second one..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.