Thread: Using Switch statement

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    1

    Smile Using Switch statement

    Dear all,

    I have one question.

    Enter a persons weight in kilograms and height in metres. Calculate the persons Quetelet Index (kolos/(meters *meters)). Print out the Quetelet Index and an appropriate message as indicated by the table below.
    Below 20 Underweight
    20 to below 35 Healthy weight
    25 to below 30 Midly overweight
    30 to below 40 Very overweight
    40 and above Extermely overweight

    I did this using If else statement but now i want to do this with switch statement.I tried but not successfull. Please help and guide.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Frankly, a switch looks inappropriate here, in my opinion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Definetely, as you'd have to test an large amount (>40) amounts of numbers. In if/else you only need 5 statements.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Hi Ubha ,

    Actually if is good here but as you want it in switch there is one way, first calculate the index, let it be in variable a, then u can write like this
    Code:
    sw=a/10;
    switch(sw)
    {
    case 0:
    case 1:
    printf("Under weight");
    break;
    case 2:
    if (a>=20 && a<25)
    printf("Healthy weight");
    if (a<=25 && a<30)
    printf("Midly weight");
    break;
    case 3:
    printf("Very overweight");
    break;
    default:
    printf("Extremely overweight");
    }
    Hope, this will solve your problem, atleast some part of it. Any comments are welcome.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    case 2:
    if (a>=20 && a<25)
    printf("Healthy weight");
    if (a<=25 && a<30)
    printf("Midly weight");
    break;
    has some problems
    Code:
    case 2:
    if (a<25)
       printf("Healthy weight");
    else
       printf("Midly weight");
    break;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Like stated, you cannot test a condtion in a switch, unless you nested the if statements
    within the switch

    Code:
    switch ( choice )
    {
    case 1:
       if ( a < b )
       // do somthing
       break;
    }
    Double Helix STL

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I have no idea what you're trying to say with the above post. The only thing wrong with their code was that their second if statement should have been testing >= instead of <=.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I was just re-iterating the example of vart, showing you can test a condition within a switch. It was not supposed to be the answer to the OP question, that had already been done
    Double Helix STL

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Quote Originally Posted by quzah
    I have no idea what you're trying to say with the above post. The only thing wrong with their code was that their second if statement should have been testing >= instead of <=.


    Quzah.

    thank you Quzah, yes I made mistake there, ye it should be the way you have stated it. Thank you once again.
    And sorry I couldn't visit you all for a long time.

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, 03: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