C – Temperature Conversion

InPursuit I’ve been having fun, reading through a couple of books. One is the second addition of K&R’s “The C Programming Language,” arguably the standard for the C programming language. Early in the book, they provide the reader with a simple conversion table between Fahrenheit and Celsius. I decided it would be fun to take their code a step further and provide a little more functionality to the program. In my spec, I would have the original table generation available but I would also provide functionality to allow the user to enter his or her own temperatures for conversion. The original table provided a one way conversion then displayed the results in a table so the user could reference the table. I had to provide calculations both ways, from Fahrenheit to Celsius and from Celsius to Fahrenheit.

Here’s the original program.

int main()
{
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
int fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;
fahr = lower;

puts("Conversion Table:\n\n");
while(fahr <= upper)
{
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
}

Here is my version. Notice I used the switch statement, evaluating characters input using getch() to determine what to do. If the user chooses anything except an invalid response or ‘q’ to quit, the appropriate function is called. Also, notice that the user input conversions get the number to convert as a string then convert the number as an int using atoi(). This is not the only way to do a conversion from string to int, nor is it usually considered the best way, since it gives no error reporting, but it is a good, quick and dirty way to do conversions of this type.

This was written and compiled as a console program using Visual Studio Express 2012. It will not compile on Linux systems without a few changes. I have a similarly working version for Linux I will post immediately following. It is not perfect and there’s a quick workaround for a problem using scanf() but it gives a decent look at using switches and functions in Linux C.

#include <stdio.h>
char getDecision()
{
char decision;

system("cls");
puts("\n\nTemperature Converter\n\n");
puts("\t(c) Celsius to Fahrenheit\n\t(f) Fahrenheit to Celsius\n\t(t) Conversion Table\n\t(q) Quit\n\nMake your selection ...\n\n");
decision = getch();

return decision;
}

void displayTable()
{
/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300 */
int fahr, celsius;
int lower, upper, step;

lower = 0;
upper = 300;
step = 20;
fahr = lower;

puts("Conversion Table:\n\n");
while(fahr <= upper)
{
celsius = 5 * (fahr-32) / 9;
printf("%d\t%d\n", fahr, celsius);
fahr = fahr + step;
}
puts("\n");
}

void convertFtoC()
{
char numberString[256];
int celsius, fahr;
// get the number to convert as a string
puts("Converting Fahrenheit to Celsius\n\n");
puts("Enter number to convert ...\n\n");
fgets (numberString, 256, stdin);
// convert the string number to an integer
fahr = atoi(numberString);
// do the calculations
celsius = 5 * (fahr-32) / 9;
// print the results
printf("%d Fahrenheit = %d Celsius\n\n", fahr, celsius);
}

void convertCtoF()
{
char numberString[256];
int celsius, fahr;

// get the number to convert as a string
puts("Converting Celsius to Fahrenheit\n\n");
puts("Enter number to convert ...\n\n");
fgets (numberString, 256, stdin);
// convert the string number to an integer
celsius = atoi(numberString);
// do the calculations
fahr = (celsius * 9) / 5 + 32;
// print the results
printf("%d Celsius = %d Fahrenheit\n\n", celsius, fahr);
}

int main()
{
char decision = 'x';

decision = 'x';

while(decision != 'q')
{
decision = getDecision();

switch(decision)
{
case 'q':
system("cls");
puts("\n\nTemperature Converter\n\n");
puts("Program exited with no errors.\n\n");
exit(0);
break;
case 'c':
convertCtoF();
break;
case 'f':
convertFtoC();
break;
case 't':
displayTable();
break;
default :
puts("Invalid selection.\n\n");
}
puts("\nPress any key to continue ...\n\n");
getch();
}
return 0;

Now, here’s the code that compiles on a Linux machine.

#include <stdio.h> 
#include <stdlib.h> 
 
char getDecision() 
{ 
    char maindecision; 
 
    system("clear"); 
    puts("\n\nTemperature Converter\n\n"); 
    puts("\t(c) Celsius to Fahrenheit\n\t(f) Fahrenheit to Celsius\n\t(t) Conversion Table\n\t(q) Quit\n\nEnter your selection ...\n\n"); 
    maindecision = getchar(); 
 
    return maindecision; 
} 
 
void displayTable() 
{ 
    /* print Fahrenheit-Celsius table 
    for fahr = 0, 20, ..., 300 */ 
    int fahr, celsius; 
    int lower, upper, step; 
 
    lower = 0; 
    upper = 300; 
    step = 20; 
    fahr = lower;             
 
    puts("Conversion Table:\n\n"); 
    while(fahr <= upper) 
    { 
        celsius = 5 * (fahr-32) / 9; 
        printf("%d\t%d\n", fahr, celsius); 
        fahr = fahr + step; 
    } 
    puts("\n"); 
} 
 
void convertFtoC() 
{ 
    char numberString[256]; 
    int celsius, fahr; 
    // get the number to convert as a string 
    system("clear"); 
    puts("Converting Fahrenheit to Celsius\n\n"); 
    puts("Enter degrees Fahrenheit ...\n\n"); 
    // get and process the number 
    if(scanf("%s", numberString)) 
    { 
        // convert the string number to an integer 
        fahr = atoi(numberString); 
        // do the calculations 
        celsius = 5 * (fahr-32) / 9; 
        // print the results 
        printf("\n\n%d Fahrenheit = %d Celsius\n\n", fahr, celsius); 
    } 
    else printf("\n\nIncorrect input received.\n\n"); 
} 
 
void convertCtoF() 
{ 
    char numberString[256]; 
    int celsius, fahr; 
 
    // get the number to convert as a string 
    system("clear"); 
    puts("Converting Celsius to Fahrenheit\n\n"); 
    puts("Enter degrees Celsius ...\n\n"); 
    // get and process the number 
    if(scanf("%s", numberString)) 
    { 
        // convert the string number to an integer 
        celsius = atoi(numberString); 
        // do the calculations 
        fahr = (celsius * 9) / 5 + 32; 
        // print the results 
        printf("\n\n%d Celsius = %d Fahrenheit\n\n", celsius, fahr); 
    } 
    else puts("\n\nIncorrect input received.\n\n"); 
} 
 
int main() 
{ 
    char decision = 'x'; 
 
    decision = 'x'; 
 
    while(decision != 'q') 
    { 
        decision = getDecision(); 
 
        switch(decision) 
        { 
            case 'q': 
                system("clear"); 
                puts("\n\nTemperature Converter\n\n"); 
                puts("Program exited with no errors.\n\n"); 
                exit(0); 
                break; 
            case 'c': 
                convertCtoF(); 
                break; 
            case 'f': 
                convertFtoC(); 
                break; 
            case 't': 
                displayTable(); 
                break; 
            default : 
                puts("Invalid selection.\n\n"); 
        } 
        getchar(); 
        puts("\nEnter to continue ...\n\n"); 
        getchar(); 
    } 
    return 0; 
} 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s