/* temp_conversion.c - Script 5.5 - Update */
#include <stdio.h>
int main(void) {
// Declaring required variables
float temp_i, temp_o;
char which_i, which_o;
char junk;
// Prompting user to gather information
printf ("Enter a temperature and indicate if it's Fahrenheit or Celcius [##.# C/F]: ");
// Reading input and storing variables - Part of conditional
if (scanf ("%f %c", &temp_i, &which_i) == 2) {
// Creating switch wherein conversion will take place
switch (which_i) {
case 'C':
case 'c':
temp_o = (temp_i * (9.0/5.0)) + 32;
which_o = 'F';
break;
case 'F':
case 'f':
temp_o = (temp_i - 32) * (5.0/9.0);
which_o = 'C';
break;
default:
which_o = 0;
break;
} // End of Switch for conversion
/* ** temp_o = temp_output vice versa for temp_i */
// Printing conversion
if (which_o) {
printf ("%0.1f %c is %0.1f %c.\n", temp_i, which_i, temp_o, which_o);
} else {printf (" You failed to enter 'C' or 'F' to indicate the current temperature.\n");
}
} else { //didn't enter right syntax
printf (" You failed to use the proper syntax.\n");
}
// Adding while loop to read any more input
do {
junk = getchar();
} while (junk != '\n' );
printf(" Please continue pressing enter to quit");
getchar();
return 0;
}