HiveBrain v1.2.0
Get Started
← Back to all entries
patterncModerate

Temperature conversion in C

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
conversiontemperaturestackoverflow

Problem

I have started to learn C and I have tried my hand at temperature conversion. The idea is to convert a temperature from Fahrenheit to Celsius and vice/versa. Can somebody review my code and let me know how I might be able to improve it? My program is as follows:

#include 

void fahconv()
{
    double fah;
    puts("Enter value to convert from Fahrenheit to Celsius");
    scanf("%lf",&fah);
    printf("Converted temperature from F to C: %lf",((fah - 32.0)*5.0)/9.0);
}

void celsconv()
{
    double cels;
    puts("Enter value to convert from Celsius to Fahrenheit");
    scanf("%lf",&cels);
    printf("Converted temperature from C to F: %lf",(cels*9/5)+32);
}

void invalidchoice()
{
    printf("Invalid choice..exiting");
}

int main()
{
    char choice;
    puts("Enter (F or f) to convert from Fahrenheit to Celsius");
    puts("Enter (C or c) to convert from Celsius to Fahrenheit \n");
    scanf("%c",&choice);
    switch(choice)
    {
        case 'c': 
        case 'C': celsconv();
              break;
        case 'f': 
        case 'F': fahconv();
              break;
        default: invalidchoice();
    }
    return 0;
}

Solution

-
You don't need invalidchoice() as it just prints a statement and nothing else. Just put the printf() under default.

-
Consider receiving all user input in main() and having the functions only do the calculations. It's best to have a function do one (useful) thing.

In this form, the functions could also return the values to main() and displayed. With the user input eliminated, they should now take the original temperature as an argument.

-
Optionally, you can return EXIT_SUCCESS or EXIT_FAILURE instead of 0 or 1, respectively. It's especially important to return a failure value if the conversion was not successful.

You could then have something like this:

#include 
#include 

double fahconv(double fah)
{
    return (fah - 32) * 5 / 9;
}

double celsconv(double cels)
{
    return (cels * 9 / 5) + 32;
}

int main()
{
    double originalTemp;
    double convertedTemp;
    char choice;

    scanf("%lf", originalTemp);

    puts("Enter (F or f) to convert from Fahrenheit to Celsius\n");
    puts("Enter (C or c) to convert from Celsius to Fahrenheit\n");
    scanf("%c", &choice);

    switch (choice)
    {
        case 'c':
        case 'C':
              convertedTemp = celsconv(originalTemp);
              printf("Converted temperature from F to C: %lf", convertedTemp);
              break;
        case 'f': 
        case 'F':
              convertedTemp = fahconv(originalTemp);
              printf("Converted temperature from F to C: %lf", convertedTemp);
              break;
        default:
              printf("Invalid choice..exiting");
              return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Code Snippets

#include <stdio.h>
#include <stdlib.h>

double fahconv(double fah)
{
    return (fah - 32) * 5 / 9;
}

double celsconv(double cels)
{
    return (cels * 9 / 5) + 32;
}

int main()
{
    double originalTemp;
    double convertedTemp;
    char choice;

    scanf("%lf", originalTemp);

    puts("Enter (F or f) to convert from Fahrenheit to Celsius\n");
    puts("Enter (C or c) to convert from Celsius to Fahrenheit\n");
    scanf("%c", &choice);

    switch (choice)
    {
        case 'c':
        case 'C':
              convertedTemp = celsconv(originalTemp);
              printf("Converted temperature from F to C: %lf", convertedTemp);
              break;
        case 'f': 
        case 'F':
              convertedTemp = fahconv(originalTemp);
              printf("Converted temperature from F to C: %lf", convertedTemp);
              break;
        default:
              printf("Invalid choice..exiting");
              return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

Context

StackExchange Code Review Q#39720, answer score: 18

Revisions (0)

No revisions yet.