Jiskha Homework Help
SATURDAY
November 21, 2009
SCHOOL SUBJECTS
- Art
- Business
- Computers
- English
- Foreign Languages
- Health
- Home Economics
- Math
- Music
- Physical Education
- Science
- Social Studies
GRADE LEVELS
- Preschool
- Kindergarten
- Elementary School
- 1st Grade
- 2nd Grade
- 3rd Grade
- 4th Grade
- 5th Grade
- 6th Grade
- 7th Grade
- 8th Grade
- High School
- 9th Grade
- 10th Grade
- 11th Grade
- 12th Grade
- College
- Adult Education
Post a New Question | Current Questions | Chat With Live Tutors

Homework Help Forum: C Programming

Posted by gnozahs on Tuesday, September 29, 2009 at 12:50am.

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 - Dean, Tuesday, September 29, 2009 at 1:05am

    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 - gnozahs, Tuesday, September 29, 2009 at 1:11am

    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 - jim, Tuesday, September 29, 2009 at 4:52am

    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 - MathMate, Tuesday, September 29, 2009 at 8:37am

    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 - gnozahs, Tuesday, September 29, 2009 at 1:49pm

    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 - MathMate, Tuesday, September 29, 2009 at 2:04pm

    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 - gnozahs, Wednesday, September 30, 2009 at 5:46pm

    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:

SEARCH

COMMUNITY
FEATURES
- Live Tutors
- Net Riddle
- Reference
- Search