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);
}
}