Switch statement help

This is a discussion on Switch statement help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I've been mucking around with this or hours, to no avail. Hope you can help. Basically, how the ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    11

    Switch statement help

    Hey guys, I've been mucking around with this or hours, to no avail. Hope you can help.

    Basically, how the crap can I shorten this block of code? It works just fine how it is, but I've been assured it is possible to cut it right down in lengh. Somehow. I have tried cutting the case statements down to 15 in number, and changing the fourth line to read "switch (speed/2)", but when 31 is entered, the program returns $310 instead of "go to jail",

    Anyway here's the source code:

    Code:
     int main()
    {int speed;
    cout << "Enter speed in km/h: "; cin >> speed;
    switch (speed)
    {case 0:
    case 1: 
    case 2:
    case 3:
    case 4:
    case 5:cout << "No fine applies" << endl; break;
    case 6: 
    case 7:
    case 8:
    case 9:
    case 10:cout << "The fine is $20" <<endl; break;
    case 11:
    case 12:
    case 13:
    case 14:
    case 15: cout << "The fine is $50" <<endl; break;
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 21:
    case 22:
    case 23:
    case 24:
    case 25:
    case 26:
    case 27:
    case 28:
    case 29:
    case 30: cout << "The fine is: $" << speed*10 << endl; break;
    default: cout << "Go to Jail\n";
    }
    Thanks in advance,
    George

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I wouldn't use a switch statement. There are just so many duplicate values. This is much easier to read:
    Code:
    if(speed < 0 || speed > 30) {
        cout << "Go to Jail\n";
    }
    else if(speed < 5) {
        cout << "No fine applies" << endl;
    }
    else if(speed < 15) {
        cout << "The fine is $50" <<endl;
    }
    else {  /* speed < 30 */
        cout << "The fine is: $" << speed*10 << endl; break;
    }
    If you have to use a switch for some reason, you could always conserve vertical whitespace at the cost of horizontal whitespace by putting multiple cases on one line.
    Code:
    case 0: case 1: case 2: /* ... */
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    Thanks for the prompt reply.

    Yes that would be somewhat easier, but the requirements for this assessment state that a Switch statement must be used. I tried a combination of Switch and 'else if', but am not sure that's allowed either.

    So, any way round this using *just* a switch statement?

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    Sorry, missed the last part of your comment.

    If all else fails I'll do that. Good idea.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by GeorgeV View Post
    Thanks for the prompt reply.

    Yes that would be somewhat easier, but the requirements for this assessment state that a Switch statement must be used. I tried a combination of Switch and 'else if', but am not sure that's allowed either.

    So, any way round this using *just* a switch statement?
    31 / 2 = 15 (in integer math).

    instead of dividing by 2, how about trying ((speed - 1) / 5).
    Last edited by Darryl; 08-06-2007 at 07:55 AM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by Darryl View Post
    31 / 2 = 15 (in integer math).

    instead of dividing by 2, how about trying ((speed - 1) / 5).
    Shh Shh, he said it was an assessment. (Of his ability, not ours)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Aug 2007
    Posts
    11
    Hah! I had that figured out earlier today, but it wouldn't work. So it turns out I had the brackets all wrong - I only had one set enclosing the entire equation.

    Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21