Using Switch statement

This is a discussion on Using Switch statement within the C Programming forums, part of the General Programming Boards category; Dear all, I have one question. Enter a persons weight in kilograms and height in metres. Calculate the persons Quetelet ...

  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
    20,959
    Frankly, a switch looks inappropriate here, in my opinion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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;
    }
    I'm just trying to be a better person - My Name Is Earl

  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,475
    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
    I'm just trying to be a better person - My Name Is Earl

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