C: Temperature Conversion With Main Repeat


 https://github.com/pereiradaniel/beginning_c/blob/master/ch3/exercises/3_1/exercise3_1.c

// Exercise 3-1

// Write a program that will first allow a user to choose one of two options:

//     1. Convert a temperature from degrees Celsius to degrees Fahrenheit.
//     2. Convert a temperature from degrees Fahrenheit to degrees Celsius.

// The program should then prompt for the temperature value to be entered and output the new value that results
// from the conversion. To convert from Celsius to Fahrenheit you can multiply the value by 1.8 and then add 32.

#include <stdio.h>
#include <ctype.h>
#define TOFARENHEIT(a) a*1.8+32.0
#define TOCELSIUS(a) (a-32.0)/1.8

int main(int argc, char* argv[])
{
    int choice = 0;     // Stores user's menu choice.
    double temp = 0.0;  // Stores user's temperature input.

    char repeat = 'Y';

    while (toupper(repeat)=='Y')
    {
        choice = 0;
        while (choice == 0)
        {
            printf("Choose 1 or 2 from the following options:\n");
            printf("\t1. Convert a temperature from degrees Celsius to degrees Fahrenheit.\n");
            printf("\t2. Convert a temperature from degrees Fahrenheit to degrees Celsius.\n");
            scanf(" %d", &choice);
            
            if (choice != 1 && choice != 2)
            {
                printf("\nInvalid selection! Choose 1 or 2. Try again.\n");
                choice = 0;
            }
        }

        printf("Enter temperature to convert:\n");
        scanf(" %lf", &temp);

        if (choice == 1)
            printf("\nConversion:\n%.2f celsius is %.2lf fahreneit.\n", temp, TOFARENHEIT(temp));
        if (choice == 2)
            printf("\nConversion:\n%.2f farenheit is %.2lf celsius.\n", temp, TOCELSIUS(temp));

        printf("\nAgain? (Y to repeat, any other key to quit.)\n");
        scanf(" %c", &repeat);
    }

    return 0;
}
https://github.com/pereiradaniel/beginning_c/blob/master/ch3/exercises/3_1/exercise3_1.txt
==19635== Memcheck, a memory error detector
==19635== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19635== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==19635== Command: ./exercise3_1
==19635== 
Choose 1 or 2 from the following options:
        1. Convert a temperature from degrees Celsius to degrees Fahrenheit.
        2. Convert a temperature from degrees Fahrenheit to degrees Celsius.
3

Invalid selection! Choose 1 or 2. Try again.
Choose 1 or 2 from the following options:
        1. Convert a temperature from degrees Celsius to degrees Fahrenheit.
        2. Convert a temperature from degrees Fahrenheit to degrees Celsius.
0

Invalid selection! Choose 1 or 2. Try again.
Choose 1 or 2 from the following options:
        1. Convert a temperature from degrees Celsius to degrees Fahrenheit.
        2. Convert a temperature from degrees Fahrenheit to degrees Celsius.
1
Enter temperature to convert:
27.0

Conversion:
27.00 celsius is 80.60 fahreneit.

Again? (Y to repeat, any other key to quit.)
y
Choose 1 or 2 from the following options:
        1. Convert a temperature from degrees Celsius to degrees Fahrenheit.
        2. Convert a temperature from degrees Fahrenheit to degrees Celsius.
2
Enter temperature to convert:
80.6

Conversion:
80.60 farenheit is 27.00 celsius.

Again? (Y to repeat, any other key to quit.)
n
==19635== 
==19635== HEAP SUMMARY:
==19635==     in use at exit: 0 bytes in 0 blocks
==19635==   total heap usage: 2 allocs, 2 frees, 2,048 bytes allocated
==19635== 
==19635== All heap blocks were freed -- no leaks are possible
==19635== 
==19635== For lists of detected and suppressed errors, rerun with: -s
==19635== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Popular posts from this blog

C programming and relevancy

Shakespeare AI: My lady is more beauteous than a rose.