Thread: Using switch statements and some other questions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

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

    & where would char choice; go?

  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 02: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
    Oct 2009
    Posts
    5
    Quote Originally Posted by gnozahs View Post
    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.

    If you've entered the temperature and then chosen it to be C, then what is the need to specify that it has to be converted into F. that is pretty obvious, right?

    The output could be in this way:
    Enter a temperature: 32.4
    Enter 'C' for Celsius or 'F' for Fahrenheit: C
    The converted temperature is: 90.3 F.

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

  10. #10
    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 03:10 PM.

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

  12. #12
    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);
    
    }

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  14. #14
    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?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

Popular pages Recent additions subscribe to a feed