Post a New Question

C Programming

posted by .

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.

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Algebra

    Celsius temperature readings can be converted to Fahrenheit readings using formula F=9/5c+32 What is the fahrenheit temp. that corresponds to each of the following Celsius temperatures -10, 0, 15, 1000?
  2. Algebra

    The folrmula F=9/5c+32 can be used to make temperature conversions with the Celsius ans Fahrenheit temperature scales. F-Fahrenheit temperature C=Celsius temperature Convert 100 degress Fahrenheit to the Celsius temperature scale. …
  3. math

    the formula C=5/9(F-32) is used to convert Fahrenheit temperature,F, to Celsius temperature,C. What temperature in Fahrenheit is equivalent to a temperature of 100 Celsius. I know that in Fahrenheit its 212 degrees, but i used it by …
  4. chemistry

    Rank the size of a change in temperature of one degree fahrenheit, one degree celsius, and one kelvin. Ok I can't seem to get this question right no matter what I do. Isn't one fahrenheit degree the largest then celsius and kelvin?
  5. C Programming

    Write a program that prompts the user to enter a temperature, followed by a letter representing Celsius (C) or Fahrenheit (F). Then prompt the user for the conversion type and print the results to the screen with one decimal point. …
  6. C Programming

    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 …
  7. math

    Consider the following method of estimating Fahrenheit temperatures given Celsius temperatures (which was stated earlier in this section): To estimate the Fahrenheit temperature for a given Celsius temperature, double the Celsius temperature …
  8. Fund of programming 1

    Complete the following C++ program. The program should compute and output the Celsius value corresponding to the given Fahrenheit value. //************************************************ // Celsius program // This program outputs …
  9. Algebra

    In order to covert temperature from Celsius to Fahrenheit,one can use the following equation f= 1.8c+32,where C is the temperature in Celsius and F is the temprature in Fahrenheit.what is the temperature in Fahrenheit when C is 15 …
  10. INTRO PROGRAMMING

    Celsius to Fahrenheit Table Design a program that displays a table of the Celsius temperatures 0 through 20 and their Fahrenheit equivalents. The formula for converting a temperature from Celsius to Fahrenheit is F = 95 C + 3 2 where …

More Similar Questions

Post a New Question