PDA

Click to See Complete Forum and Search --> : C


Robert Briggs
Dec 15th, 2000, 08:09 AM
Can anyone help I have a program to write

I need to write a pgm that converts a series of input temperatures in either degree Celsius or Fahrenheit and calculate the absolute temperature in degrees Kelvin.
So if the input was 32F the result would be 273K.Repeat this process for any number of input temperatures and scales.
The pgm should finish with the following message "Error - Negative Absolute Value do not assume the input will only be 'C' OR 'F'.
The required formulae are
K = C + 273 or K = (F-32) / 1.8 + 273

Can anyone help! I'me slowly going insane......

HarryW
Dec 15th, 2000, 09:20 AM
Well if you define the way you want the thing to work then this is an easy program. Something like this:


#include <stdio.h>
#include <stdlib.h>

void typeError();

void main()
{ int temp;
char type;

printf("What unit are you going to enter a temperature in? (C/F/K) ");
scanf("%c", &type);

printf("\n\nEnter an integer value for your temperature: ");
scanf("%d", &temp);

printf("\n\n");

switch(tolower(type))
{ case 'c':
temp = temp + 273;
break;
case 'f':
temp = (temp-32)*5/9 + 273;
break;
case 'k':
break;
default:
typeError();
}

printf("Which unit would you like this temperature in? (C/F/K) ");
scanf("%c", &type);

switch(tolower(type))
{ case 'c':
temp = temp - 273;
break;
case 'f':
temp = (temp-241)*9/5;
break;
case 'k':
break;
default:
typeError();
}

printf("%d %c\n\n", temp, toupper(type));
}

void typeError()
{ printf("Error: invalid unit; must be C, F or K\n\n");
exit(1);
}


Note that this is C code. C++ is easier but you said C, so you got C :)

Dec 15th, 2000, 09:54 AM
'scuse me, but toupper() and tolower() are in ctype.h

HarryW
Dec 15th, 2000, 11:41 AM
Good call D, sorry 'bout that. My bad.

you'll need to add at the top:
#include <ctype.h>