using switch and if/else statements... need HELP!

This problem is about a car rental company.

Page 1 of 1

14 Replies - 1014 Views - Last Post: 06 March 2010 - 09:29 PM Rate Topic: -----

#1 Guest_Lindsey*


Reputation:

using switch and if/else statements... need HELP!

Posted 06 March 2010 - 06:57 PM

#include <iostream>

using namespace std;

int main()
{
    //variables
    int mileage;
    char type;
    int owedPrice;

    cout << "Enter Sub-Compact, Sedan, SUV, or Van: ";
    cin >> type;

    switch (type)

    case Sub-Compact: .25 per mile;
                    break;
    case Sedan: .50 per mile;
                break;
    case Suv: .75 per mile;
            break;
    case Van: 1.00 per mile;

    cout << "How many miles have you driven? ";
    cin >> mileage;

    if (mileage <=100)
    {
        owed = car * 100;
    }
    else if (mileage > 100)
    {
        owed = car * 100 + (car * .10)(mileage - 100);
    }

    cout << "Your total price owed is: " << owedPrice << endl;

    owedPrice = type * mileage;


    return 0;

This post has been edited by JackOfAllTrades: 06 March 2010 - 07:10 PM
Reason for edit:: Added code tags. PLEASE, FOR THE LOVE OF ALL...[code]...POST YOUR CODE IN HERE!!!...[/code]


Is This A Good Question/Topic? 0

Replies To: using switch and if/else statements... need HELP!

#2 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:00 PM

Please post your problem in the body of the post, instead of the title, and remember to use the code tags, like so:
:code:

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 Guest_Lindsey*


Reputation:

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:02 PM

#include <iostream>

using namespace std;

int main()
{
    //variables
    int mileage;
    char type;
    int owedPrice;

    cout << "Enter Sub-Compact, Sedan, SUV, or Van: ";
    cin >> type;

    switch (type)

    case Sub-Compact: .25 per mile;
                    break;
    case Sedan: .50 per mile;
                break;
    case Suv: .75 per mile;
            break;
    case Van: 1.00 per mile;

    cout << "How many miles have you driven? ";
    cin >> mileage;

    if (mileage <=100)
    {
        owed = car * 100;
    }
    else if (mileage > 100)
    {
        owed = car * 100 + (car * .10)(mileage - 100);
    }

    cout << "Your total price owed is: " << owedPrice << endl;

    owedPrice = type * mileage;


    return 0;


Was This Post Helpful? 0

#4 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:04 PM

Please don't double post. There is an edit button available for your posts for a reason. You also still didn't tell us what the problem was!
Was This Post Helpful? 0
  • +
  • -

#5 Guest_lindsey*


Reputation:

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:06 PM

View Posterik.price, on 06 March 2010 - 06:04 PM, said:

Please don't double post. There is an edit button available for your posts for a reason. You also still didn't tell us what the problem was!


Quote

The problem is that it will not run correctly and I do not get switch statements. I am pretty sure that I have the if/else statement correct.

Was This Post Helpful? 0

#6 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:10 PM

case Sub-Compact: .25 per mile;
                    break;
    case Sedan: .50 per mile;
                break;
    case Suv: .75 per mile;
            break;
    case Van: 1.00 per mile;



What is the 'per' supposed to mean here? If you wanted to use division, you would use '/'
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:11 PM

 case Sub-Compact: .25 per mile;
                    break;
    case Sedan: .50 per mile;
                break;
    case Suv: .75 per mile;
            break;
    case Van: 1.00 per mile;


That's not C or C++. It's English. And "Sub-Compact" is not a legal token in C/C++.
Was This Post Helpful? 0
  • +
  • -

#8 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:31 PM

I told you how to fix this problem in your other topic.

Read what I posted and apply the changes. It will work, I tested it.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Lindsey*


Reputation:

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:35 PM

View Posteker676, on 06 March 2010 - 06:31 PM, said:

I told you how to fix this problem in your other topic.

Read what I posted and apply the changes. It will work, I tested it.


Quote

I have looked for my other topic and for some reason can't find it. Will you please post it again?

Was This Post Helpful? 0

#10 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:42 PM

My other post:

Quote

Your problem is using the integer carType to store a double.

To counter this problem you can use carType to get the type of the car and for the switch statement. Then use a double variable called carFactor for the calculation.


double carFactor = 0.0;
  
    switch (carType )
    {
        case 1: carFactor = .25;
        cout<< " your choice was a subCompact ";
        break;
        case 2: carFactor = .50;
        cout<< " your choice was a sedan ";
        break;
        case 3: carFactor = .75;
        cout<< " your choice was a suv ";
        break;
        case 4: carFactor = 1.00;
        cout<< " your choice was a van ";
    }
    
    // processing

    if (milesDriven <= 100)
        totalPriceOwed = milesDriven * carFactor;
    else
    {
        additionalMileage = (milesDriven - 100);
        totalPriceOwed =  additionalMileage * (carFactor + .10) + (milesDriven * carFactor);
    }


Take the code you had before and replace the switch statement and the if conditionals with the one I posted above.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_lindsey*


Reputation:

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 07:55 PM

#include <iostream>

using namespace std;

int main()
{
    //variables
    int milesDriven;
    int carType;
    int totalPriceowed;
    int additionalMileage;

    cout << "Enter Compact, Sedan, Suv, or Van: ";
    cin >> carType;

    double carFactor = 0.0;

    switch (carType)
        case 1: carFactor = .25;
        cout<< " your choice was a subCompact ";
        break;
        case 2: carFactor = .50;
        cout<< " your choice was a sedan ";
        break;
        case 3: carFactor = .75;
        cout<< " your choice was a suv ";
        break;
        case 4: carFactor = 1.00;
        cout<< " your choice was a van ";

    cout << "How many miles have you driven? ";
    cin >> milesDriven;

    if (milesDriven <= 100)
        totalPriceowed = milesDriven * carFactor;
    else
    {
        additionalMileage = (milesDriven - 100);
        totalPriceowed =  additionalMileage * (carFactor + .10) + (milesDriven * carFactor);
    }


    return 0;
}


Quote

I keep getting errors about the break statements and the case labels aren't within the switch statement.

Was This Post Helpful? 0

#12 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 08:00 PM

You forgot the {} brackets for your switch statement.

switch
{
CODE HERE
}

Also, you didn't print out the amount owed.
Was This Post Helpful? 0
  • +
  • -

#13 Guest_lindsey*


Reputation:

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 08:08 PM

View Posteker676, on 06 March 2010 - 07:00 PM, said:

You forgot the {} brackets for your switch statement.

switch
{
CODE HERE
}

Also, you didn't print out the amount owed.

Quote

Well now the program will run but it asks for the user to enter type of car and when I do it skips asking for the number of miles. The question is there but it skips you being able to enter the amount and goes to the total price owed with a big number.

Was This Post Helpful? 0

#14 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 08:32 PM

Well, you have to enter 1, 2, 3, or 4 when it asks for carType in your program.

I tried it with a number between 1 and 4 and it works fine.

I'm guessing that you are trying to write out Sub-Compact or Van, etc. It will skip the number of miles because you are giving your program garbage data. It wants and integer, you can't give it a string.
Was This Post Helpful? 0
  • +
  • -

#15 zim1985   User is offline

  • Grand Inquisitor
  • member icon

Reputation: 75
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: using switch and if/else statements... need HELP!

Posted 06 March 2010 - 09:29 PM

View Posteker676, on 06 March 2010 - 06:32 PM, said:

Well, you have to enter 1, 2, 3, or 4 when it asks for carType in your program.

I tried it with a number between 1 and 4 and it works fine.

I'm guessing that you are trying to write out Sub-Compact or Van, etc. It will skip the number of miles because you are giving your program garbage data. It wants and integer, you can't give it a string.

Yes, switch statements only work with ints, feed it ints nao! IT'S HUNGRY FOR INTS!

Sorry, "it wants an integer" made me want to do that. But seriously, print a table with numbered options, have the user type one of the options and then plug that into the switch statement.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1