Hello
I'm currently working on a program that when you input a number in Kelvin, it will give u the temperature in either Farenheit or Celcius then tell you what state its in (liquid, solid, gas)
Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <ctype.h>
double readnumber(void)
{
char ch[10];
double input;
printf("\n");
printf("Enter a temperature in Kelvin:\n");
fflush(stdout);
fgets(ch,sizeof ch,stdin);
input=atof(ch);
return(input);
}
int temptype(void)
{
char ch[10];
char option;
do {
printf("\n");
printf("Enter 'F' for Fahrenheit or 'C' for Celcius:\n");
option=getch();
option=tolower(option);
}while(!((option == 'f')||(option == 'c')));
return(option);
}
int fahrenheit(input)
{
double inpF;
int type;
inpF=((9*(input-273))/5)+32;
if (inpF>=212) type = 4;
else if (inpF>=33 && inpF<=211) type = 5;
else if (inpF<=32) type = 6;
return(type);
}
int celcius(input)
{
double inpC;
int type;
inpC=input-273;
if (inpC>=101) type = 1;
else if (inpC>=1 && inpC <= 100) type = 2;
else if (inpC<=0) type = 3;
return(type);
}
int main(void)
{
int type;
double input=0.0;
char option, opt;
printf("This is the temperature program!");
do
{
printf("\n\nDo you wish to continue? (y/n): ");
do
{
opt = getch();
opt = tolower (opt);
}while (!((opt=='y')||(opt=='n')));
printf("%c\n",opt);
if (opt=='y')
{
input = readnumber();
option = temptype();
if(option == 'c')
{
type = celcius(input);
}
else if(option == 'f')
{
type = fahrenheit(input);
}
switch (type)
{
case 1:
printf("\nThe state of the water is: Gas, at %.2f degrees Celcius.\n", input);
break;
case 2:
printf("\nThe state of the water is: Liquid, at %.2f degrees Celcius.\n", input);
break;
case 3:
printf("\nThe state of the water is: Solid, at %.2f degrees Celcius.\n", input);
break;
case 4:
printf("\nThe state of the water is: Gas, at %.2f degrees Fahrenheit.\n", input);
break;
case 5:
printf("\nThe state of the water is: Liquid, at %.2f degrees Fahrenheit.\n", input);
break;
case 6:
printf("\nThe state of the water is: Solid, at %.2f degrees Fahrenheit.\n", input);
break;
}
}
}
while (opt!='n');
return(0);
}
atm though, it doesn't matter what temperature it is, it will always be a solid and then always end up being the same answer, every time when I go to repeat the loop