Saturday
March 25, 2017

Post a New Question

Posted by on .

Okay, question.

What if I inputted this:
Enter a temperature: 32
Enter 'C' for Celsius or 'F' for Fahrenheit: c
Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): c
The temperature is: 0.0 C.

I want the program to read [The temperature is: 32 C.] when I go from C to C but my program is outputting 0.0 C. Same thing when I go from F to F.

CODE:
/* Directives */

#include <stdio.h>

int main(void)
{

float temp, CtoF, FtoC;
char choic;
char choice;
char answer[10];
char answers[10];
printf("Enter a temperature: ");
scanf("%f", &temp);

printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");

scanf("%s", answer);
choic=answer[0];

switch(choic)
{
case 'F':
case 'f':
{
break;
}

case 'C':
case 'c':
{
break;
}

default:
{printf("Unknown Syntax!\n");
return(0);
break;
}
}


printf("Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): ");


scanf("%s", answers);
choice=answers[0];

switch(choice)
{
case 'F':
case 'f':
{CtoF = temp*9/5+32;
printf("The temperature is: %.1f F.\n", CtoF);
break;
}
case 'C':
case 'c':
{FtoC = (temp - 32) * (5.0/9.0);
printf("The temperature is: %.1f C.\n", FtoC);
break;
}

default:
{printf("Unknown Syntax!\n");
return(0);
break;
}
}





/* End Program */
return(0);

}

  • C Programming - ,

    This is a homework helping site, we can't help you with your programming issues, I believe, discuss that with the founder of that program.

    I would help you with this, but Its too tiring. (coding)

    float temp, CtoF, FtoC;
    char choic;
    char choice;
    char answer[10];
    char answers[10];
    printf("Enter a temperature: ");
    scanf("%f", &temp);

    printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");

    scanf("%s", answer);
    choic=answer[0];

    ^ above, fix the coding, it isn't correct. "answer[0]" I believe.

  • C Programming - ,

    MathMate was helping me with it the other day. Everything works correctly except for when I convert it from Celsius to Celsius (same thing), I'm wondering how I would make a code to make it output the same input number since it's going to be the same temperature.

  • C Programming - ,

    Your problem is that you are always doing a CtoF or FtoC conversion, even when it's F to F or C to C. You need to take both choices into account in your conversion logic, not just the one you're converting to.

    Actually, your approach is a bit convoluted. It can be done more simply in other ways - like, simply assume that the user isn't converting F to F, and take the second choice for granted! Like, if he's converting from F, don't ask what he's converting to, just assume it's C.

    However, following on with the approach you have taken, you need a new if/else (or switch statement) within your conversion logic, to decide whether to apply a conversion or not.

    So your switches now are:

    C to F
    F to C
    F to F
    C to F

    and you have four assignments depending on which was chosen.

    The patch below follows from your work so far, but I say again that standing back from the problem and thinking about it differently will produce a shorter program, and one easier to understand.

    /* Directives */

    #include <stdio.h>

    int main(void)
    {

    float temp, CtoF, FtoC;
    char choic;
    char choice;
    char answer[10];
    char answers[10];

    printf("Enter a temperature: ");
    scanf("%f", &temp);

    printf("Enter 'C' for Celsius or 'F' for Fahrenheit: ");

    scanf("%s", answer);
    choic=answer[0];

    switch(choic)
    {
    case 'F':
    case 'f':
    {
    break;
    }

    case 'C':
    case 'c':
    {
    break;
    }

    default:
    {printf("Unknown Syntax!\n");
    return(0);
    break;
    }
    }


    printf("Choose a conversion ('C' for Celsius or 'F' for Fahrenheit): ");


    scanf("%s", answers);
    choice=answers[0];

    switch(choice)
    {
    case 'F':
    case 'f':
    {
    switch(choic)
    {
    case 'F':
    case 'f':
    {
    CtoF = temp; /* converting F to F */
    break;
    }
    case 'C':
    case 'c':
    {
    CtoF = temp*9/5+32; /* converting C to F */
    break;
    }
    }
    printf("The temperature is: %.1f F.\n", CtoF);
    break;
    }
    case 'C':
    case 'c':
    {
    switch(choic)
    {
    case 'F':
    case 'f':
    {
    FtoC = (temp - 32) * (5.0/9.0); /* converting F to C */
    break;
    }
    case 'C':
    case 'c':
    {
    FtoC = temp; /* converting C to C */
    break;
    }
    }
    printf("The temperature is: %.1f C.\n", FtoC);
    break;
    }

    default:
    {printf("Unknown Syntax!\n");
    return(0);
    break;
    }
    }
    }

  • C Programming - ,

    I was not able to read or post any more on the same thread. Perhaps there was a limit on the total size of the thread.

    Back to your question. As Jim commented, it is normal not to consider the trivial conversions in programming, but since it is a requirement, you will have to live with it.

    The way to do this is to store the original unit (choic) and make a decision when you have obtained the destination unit (choice). The pseudocode is as follows:

    input choic (C or F) (initial unit)
    input choice (C or F) (destination unit)
    switch(choice)
    {
    case 'C':
    if(choic=='C')print input value (C)
    else print converted value (F)
    break;
    case 'F':
    if(choic=='F')print input value (F)
    else print converted value (C)
    break;
    }
    I assume you have learned the if...else construct.
    Can you work with that?

  • C Programming - ,

    Yes, I have learned the if else statements. I made another switch statement within the first switch statement to account for the F to F and C to C and that did the trick. Thanks.

  • C Programming - ,

    That's great!

    Now is the time to write a good documentation for the program.

    Programmers are often accused of not writing documentation, or writing it badly or inaccurtely. You can change this!

  • C Programming - ,

    Yeah! I keep forgetting to write comments for each part of my code. But I'll make sure I write up good comments for the code.

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question