Using switch statements and some other questions

This is a discussion on Using switch statements and some other questions within the C Programming forums, part of the General Programming Boards category; This is what the program needs to do: Enter a temperature: 32.4 Enter 'C' for Celsius or 'F' for Fahrenheit: ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    36

    Using switch statements and some other questions

    This is what the program needs to do:

    Enter a temperature: 32.4
    Enter 'C' for Celsius or 'F' for Fahrenheit: C
    Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): F
    The temperature is: 90.3 F.

    How would I store the input when it asks for C or F, then use it to activate the correct equation to convert the inputted temp to C or F?

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    char choice;
    if (choice == 'C'){
    }

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    What if I wanted to use switch statements for this program?

    & where would char choice; go?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    5

    Wink

    Code:
    char choice;
    
    choice=getchar();
    
    switch(choice)
    {
    case 'C':
    {//equation for Celcius
    break;
    }
    
    case 'F':
    {
    //equation for Farenhite
    break;
    }
    
    default:
    {
    //code for default
    break;
    }
    
    }

    A switch statement took a variable or a constant and compare the value with the case,the value then invoke the corresponding case.If no cases matches the default will invoke.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Thanks aaa111. That's what I need. Something to get me going.

    What if I wanted to make the program recognize lowercase 'c' and 'f' as uppercase 'C' and 'F'? Would I just add another case 'f' and another case 'c'?
    Last edited by gnozahs; 09-25-2009 at 03:35 PM.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    5
    Quote Originally Posted by gnozahs View Post
    Thanks aaa111. That's what I need. Something to get me going.

    What if I wanted to make the program recognize lowercase 'c' and 'f' as uppercase 'C' and 'F'? What kind of statement or function would I need to use?
    well you can use two case in a row for same thing,like this:

    Code:
    char choice;
    
    choice=getchar();
    
    switch(choice)
    {
    case 'C':
    case 'c':
    {//equation for Celcius
    break;
    }
    
    case 'F':
    case 'f':
    {
    //equation for Farenhite
    break;
    }
    
    default:
    {
    //code for default
    break;
    }
    
    }

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Wait, is the code you wrote above for storing the inputted number as C or F or actually converting it from C to F or F to C? I'm confused.

    This is what the program should look like:
    Enter a temperature: 32.4
    Enter 'C' for Celsius or 'F' for Fahrenheit: C
    Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): F
    The temperature is: 90.3 F.

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    you can also use strcmp

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    I haven't learned strcmp yet so I have no idea how to use it.

    How would I store the inputted number as a C or F temperature?
    Use scanf under the Enter 'C' for Celsius or 'F' for Fahrenheit: C?
    Last edited by gnozahs; 09-26-2009 at 04:10 PM.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Using any of the input functions, like scanf (for instance).

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Okay, so here is my code so far.

    I'm not sure how to connect the inputted C or F Enter 'C' for Celsius or 'F' for Fahrenheit: C to the inputted temperature Enter a temperature: 32.4 and then take all that and make it use the correct temperature conversion.
    Code:
    /* This program inputs a temperature with a letter representing Celsius */
    /* or Fahrenheit then the program converts it to either Celsius or      */
    /* Fahrenheit.                                                          */
    
    /* Directives */
    
    #include <stdio.h>
    
    int main(void)
    {
    
    float temp, fahrenheit;
    
    printf("Enter a temperature: ");
    scanf("%f", &temp);
    printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");
    
    switch (code)
    {
    case F:
    case f:
    scanf("%f", &fahrenheit);
    
    
    
    
    
    
    
    
    
    /* End Program */
    return(0);
    
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by gnozahs View Post
    Okay, so here is my code so far.

    I'm not sure how to connect the inputted C or F Enter 'C' for Celsius or 'F' for Fahrenheit: C to the inputted temperature Enter a temperature: 32.4
    You shouldn't. They are not connected, so there's no point in trying to connect them. Whether they say C or F, you still need to input a temperature. The only thing that will change is what to do with it.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Quote Originally Posted by tabstop View Post
    You shouldn't. They are not connected, so there's no point in trying to connect them. Whether they say C or F, you still need to input a temperature. The only thing that will change is what to do with it.
    Oh! I just noticed that right now.

    Alright, so how would I say Unknown Syntax! if the user inputted something other than a C or F in the part where the program asks Enter 'C' for Celsius or 'F' for Fahrenheit? Would I have to do a switch statement here too so that I can let the program know that a lowercase c is the same thing as an uppercase C?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by gnozahs View Post
    Oh! I just noticed that right now.

    Alright, so how would I say Unknown Syntax! if the user inputted something other than a C or F in the part where the program asks Enter 'C' for Celsius or 'F' for Fahrenheit? Would I have to do a switch statement here too so that I can let the program know that a lowercase c is the same thing as an uppercase C?
    That's part of your switch statement.

  15. #15
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Alright. I think I'm almost there. I'm just having a slight problem when I'm running the program.

    This is how it should look like (for reference).

    Enter a temperature: 32.4
    Enter 'C' for Celsius or 'F' for Fahrenheit: C
    Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): F
    The temperature is: 90.3 F.

    When I run it, after I enter 32.4, the next line automatically reads Enter 'C' for Celsius or 'F' for Fahrenheit: Unknown Syntax! without me having a chance to enter C or F in. And then it just takes me to the next line. I do something wrong in my switch statement?

    Here's my code:
    Code:
    /* Directives */
    
    #include <stdio.h>
    
    int main(void)
    {
    
    float temp, CtoF, FtoC;
    
    printf("Enter a temperature: ");
    scanf("%f", &temp);
    printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");
    
    char choic;
    choic=getchar();
    
    switch(choic)
    {
    case 'F':
    case 'f':
    {break;
    }
    
    case 'C':
    case 'c':
    {break;
    }
    
    default:
    {printf("Unknown Syntax!\n");
    break;
    }
    }
    
    
    printf("Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): ");
    
    char choice;
    choice=getchar();
    
    switch(choice)
    {
    case 'F':
    case 'f':
    {CtoF = (temp - 32) * (5.0/9.0);
    printf("The temperature is: %.1f F.\n", CtoF);
    break;
    }
    case 'C':
    case 'c':
    {FtoC = temp * (5.0/9.0) + 32; 
    printf("The temperature is: %.1f C.\n", FtoC);
    break;
    }
    
    default:
    {printf("Uknown Syntax!\n");
    break;
    }
    }
    
    
    
    
    
    /* End Program */
    return(0);
    
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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