Question: I have this C program that converts temperature both from farenheit-to-celcius and celcius-to-farenheit. Asks the user for number of steps (between 1-9) and prints out
I have this C program that converts temperature both from farenheit-to-celcius and celcius-to-farenheit.
Asks the user for number of steps (between 1-9) and prints out a table of both the temperature conversions with the number of steps indicated by the user in between the numbers to be converted. For example if the user input is 5 it would print the following:
Fahrenheit Celsius Celsius Fahrenheit
-80.0 -62.2 -80.0 -112.0
-75.0 -59.4 -75.0 -103.0
-70.0 -26.7 -70.0 -94.0
PROBLEMS:
1. Add a blank line after every 8 lines in the table.
2. Input Validation, prompt the user that the input is invalid if the user types a string or a character.
/***************************************************/ #include /* program defines */
#define TRUE 1 /* boolean true */ #define FALSE 0 /* boolean false */ #define VALID 1 /* valid status */ #define ERRORS 1 /* errors found */ #define NOERRORS 0 /* no errors found */ #define TEMP_MIN -20.0 /* min. temperature for table*/ #define TEMP_MAX 280.0 /* max. temperature for table*/ #define VALID_INPUT_MIN 1 /* min. number acceptable input */ #define VALID_INPUT_MAX 9 /* min. number acceptable input */
/***************************************************/ /* function prototypes */
float convertToCelsius(float); float convertToFahr(float); int isValidInput(int);
/***************************************************/
int main(int argc, char *argv[]) { /* Declare variables and initialize */ int step = 0; float temp = 0; float fahr = 0; float celsius = 0; /* Prompt the user to enter number of steps */ printf("Please enter the the conversion step: "); step = getchar() - '0'; if(isValidInput(step) == TRUE){
/* Print the header of the table */ printf(" Fahrenheit\tCelsius\t\tCelsius\t\tFahrenheit "); printf("----------------------------------------------------------- "); temp = TEMP_MIN; while(temp < TEMP_MAX){ fahr = convertToFahr(temp); celsius = convertToCelsius(temp); printf("%8.1f\t%6.1f\t%14.1f\t%16.1f ",temp,celsius,temp,fahr); temp += step; } }else{ printf(" Invalid input, make sure you are entering a single digit number" " VALID_INPUT_MIN - VALID_INPUT_MAX "); } return NOERRORS; }
float convertToCelsius(float fahr){ return (((fahr - 32) * 5) / 9); }
float convertToFahr(float celsius){ return (((celsius * 9) / 5) + 32); }
int isValidInput(int input){ if((input >= VALID_INPUT_MIN) && (input <= VALID_INPUT_MAX)){ return TRUE; }else { return FALSE; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
