Hello all,

I've had some java experience and looking at my uni units next semester, 1 involves using C, so I decided to start coding C :p. Is there anything wrong with the program structure below? I think there is also some code duplication in there also.

#include <stdio.h>

/*
This program is designed to convert kelvin into
fahrenhiet or celsius.
*/

int main()
{
   int kelvin;
   char type;

   printf("Welcome to the temperature conversion program!\n");

   readTemp(kelvin);
   readType(type);
}

void readType(char type)
{
    int kelvin, celsius, fahrenhiet;

    printf("Do you wish to convert the temperature to (c) for Celsius, or (f) for Fahrenheit");
    getchar();
    scanf("%c", &type);
    switch(type)
    {
        case 'c' :

        celsius = convertC(kelvin, celsius);
        break;

        case 'f' :
        fahrenhiet = convertF(fahrenhiet, celsius, kelvin);
        break;

        default:
        printf("You did not enter correct letter!");
        break;
    }
}

void convertC(int kelvin, int celsius)
{
    (celsius = (kelvin - 273));
     if(celsius < 1)
        {
            printf("The water is in a solid state at %d degrees celsius.\n", celsius);
        }
        else if(celsius > 0 && celsius < 100)
        {
            printf("The water is in a liquid state at %d degrees celsius.\n", celsius);
        }
        else
        {
            printf("The water is in a gas state at %d degrees celsius.\n", celsius);
        }
}

void convertF(int fahrenhiet, int celsius, int kelvin)
{
    (celsius = (kelvin - 273));
    (fahrenhiet = (9 * celsius / 5) + 32);
     if(fahrenhiet < 33)
        {
            printf("The water is in a solid state at %d degrees fahrenhiet.\n", fahrenhiet);
        }
        else if(fahrenhiet > 32 && fahrenhiet < 212)
        {
            printf("The water is in a liquid state at %d degrees fahrenhiet.\n", fahrenhiet);
        }
        else
        {
            printf("The water is in a gas state at %d degrees fahrenhiet.\n", fahrenhiet);
        }
}

void readTemp(int kelvinNumber)
{
    int kelvin, *ptr;
    char type;

    ptr = &kelvin;

    printf("Please enter a the sample temperature in Degrees Kelvin:>345\n");
    scanf("%d", &kelvin);
    if(&kelvin <= 345)
    {
        printf("You did not enter the correct value\n");
        exit(0);
    }
    else
    {
        printf("You entered %dK\n", *ptr);
    }
}

Edited 6 Years Ago by ice_cool: n/a

1) functions have to be declared before they can be called. You need to add function prototypes before the main() function. For example: void readTemp(int kelvinNumber); now do the same thing with all the other functions.

2) There is no reason for main() to delare the two variables on lines 10 and 11 since main() does nothing with them. Just remove the parameter from the two functions and declare the variables inside the two functions that are called from main().

Follow what AD has told. I just wanted to add up a bit more things...
- Same as AD's second point you you can remove the variables in line no 21 and declare it inside the converC and converF functions as they are only use locally.
- The checking you are doing for temperature value to be <= 345 in the readTemp function should be corrected to

if(kelvin <= 345) \\ no & before kelvin

- Moreover you are putting exit(0); for wrong entry from user, which you can substitute with a while loop that will continue giving chance to the user to enter correct value. Try it !!! (Same is applicable to the user input in the readType function)
- Why have you provided both

getchar(); 
scanf("%c", &type);

in the readType function ???
- To make the program more compact you can try declaring the constant values as Macros.
Ex:

#define MAX_CELSIUS 273

Happy coding... :)

Edited 6 Years Ago by kings_mitra: n/a

This question has already been answered. Start a new discussion instead.