patterncMinorCanonical
Simple Temperature Converter 2 in C
Viewed 0 times
simpletemperatureconverter
Problem
I finally got around to redoing my Temperature conversion program. I incorporated many elements of everyone's comments, and am uploading the finished program. Any new, or problems that I missed? Maybe a few very late here where I am working. Main focus of this redo was keeping my functions short and single purposed.
```
#include
void inter_face();
void welcome();
void option_prompt();
void input_temp_prompt();
int get_option();
float get_input_temp();
void display_F_to_C(float);
void display_C_to_F(float);
void display_off();
void display_error();
float F_to_C(float);
float C_to_F(float);
enum { F_TO_C = 1, C_TO_F = 2, OFF = 3 } option_type;
int main()
{
inter_face();
getchar();
}
void inter_face()
{
int option = -1;
float input_temp = 0.0;
welcome();
while (option != OFF) {
option_prompt();
option = get_option();
if (option == F_TO_C || option == C_TO_F) {
input_temp_prompt();
input_temp = get_input_temp();
}
switch (option) {
case F_TO_C:
display_F_to_C(F_to_C(input_temp));
break;
case C_TO_F:
display_C_to_F(C_to_F(input_temp));
break;
case OFF:
display_off();
break;
default:
display_error();
}
}
}
void welcome()
{
printf("Temperature conversion Calculator!!\nPlease select an option:");
printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
}
void option_prompt()
{
printf("Option: ");
}
void input_temp_prompt()
{
printf("Temp: ");
}
float get_input_temp()
{
float input_temp = 0;
scanf_s("%f", &input_temp);
return input_temp;
}
int get_option()
{
int option;
scanf_s("%d", &option);
return option;
}
void display_F_to_C(float converted_temp)
{
printf("Celsius: %f\n", converted_temp);
}
void display_C_to_F(float converted_temp)
{
printf("Fahrenheit: %f\n", converted_temp);
}
void display_o
```
#include
void inter_face();
void welcome();
void option_prompt();
void input_temp_prompt();
int get_option();
float get_input_temp();
void display_F_to_C(float);
void display_C_to_F(float);
void display_off();
void display_error();
float F_to_C(float);
float C_to_F(float);
enum { F_TO_C = 1, C_TO_F = 2, OFF = 3 } option_type;
int main()
{
inter_face();
getchar();
}
void inter_face()
{
int option = -1;
float input_temp = 0.0;
welcome();
while (option != OFF) {
option_prompt();
option = get_option();
if (option == F_TO_C || option == C_TO_F) {
input_temp_prompt();
input_temp = get_input_temp();
}
switch (option) {
case F_TO_C:
display_F_to_C(F_to_C(input_temp));
break;
case C_TO_F:
display_C_to_F(C_to_F(input_temp));
break;
case OFF:
display_off();
break;
default:
display_error();
}
}
}
void welcome()
{
printf("Temperature conversion Calculator!!\nPlease select an option:");
printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
}
void option_prompt()
{
printf("Option: ");
}
void input_temp_prompt()
{
printf("Temp: ");
}
float get_input_temp()
{
float input_temp = 0;
scanf_s("%f", &input_temp);
return input_temp;
}
int get_option()
{
int option;
scanf_s("%d", &option);
return option;
}
void display_F_to_C(float converted_temp)
{
printf("Celsius: %f\n", converted_temp);
}
void display_C_to_F(float converted_temp)
{
printf("Fahrenheit: %f\n", converted_temp);
}
void display_o
Solution
Firstly, I don't quite understand why are you declaring so much functions that consist of a single line dedicated to printing a string. They are only used once and in one file, so there's no need to use a function or constant. I suggest you to get rid of them and replace calls to these functions with their bodies. Also, put
Secondly, I recommend to use
Thirdly, I'd suggest to move the body of
Fourthly, you should put function prototypes, enumerations and other objects that carry data (Of course, prototypes do not carry data, yet they belong in header files) into a separate header file (For instance,
Fifthly, I'd consider either merging or getting rid of
```
#include "main.h" // Prototypes and enum are there!
float F_to_C(float input_temp)
{
return (5.0 / 9.0) * (input_temp - 32);
}
float C_to_F(float input_temp)
{
return (9.0 / 5.0) * (input_temp)+32;
}
float get_input(void)
{
float input = 0;
scanf_s("%f", &input);
return input;
}
int main()
{
int option = -1;
float input_temp = 0.0;
printf("Temperature conversion Calculator!!\nPlease select an option:");
printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
while (option != OFF) {
printf("Option: ");
option = get_input();
break; inside default case (Here you can learn more about the reason why you should do this):switch (option) {
case F_TO_C:
printf("Celsius: %f\n", F_to_C(input_temp));
break;
case C_TO_F:
printf("Fahrenheit: %f\n", C_to_F(input_temp));
break;
case OFF:
printf("OFF\n");
break;
default:
printf("Incorrect input, try again\n");
break;
}Secondly, I recommend to use
puts() instead of printf() when printing simple strings as it is obviously simpler and places a newline automatically (And you use it often throughout your code). Note that this doesn't involve cases when printing formatted strings.switch (option) {
case F_TO_C:
printf("Celsius: %f\n", F_to_C(input_temp));
break;
case C_TO_F:
printf("Fahrenheit: %f\n", C_to_F(input_temp));
break;
case OFF:
puts("OFF"); // No need for printf()!
break;
default:
puts("Incorrect input, try again"); // No need for printf() as well!
break;
}Thirdly, I'd suggest to move the body of
inter_face() function to main() as it is not used anywhere else and basically creates a run loop, so I don't see any reasons why you should place it in a separate function. In addition to this, you probably want to define your functions before they're used, thus moving all functions above main(). It's not necessary as the compiler will find them anyway, yet I reckon the source code looks better that way (Just imagine reading a book from the end to the beginning).float F_to_C(float input_temp)
{
return (5.0 / 9.0) * (input_temp - 32);
}
float C_to_F(float input_temp)
{
return (9.0 / 5.0) * (input_temp)+32;
}
float get_input_temp()
{
float input_temp = 0;
scanf_s("%f", &input_temp);
return input_temp;
}
int get_option()
{
int option;
scanf_s("%d", &option);
return option;
}
int main()
{
int option = -1;
float input_temp = 0.0;
printf("Temperature conversion Calculator!!\nPlease select an option:");
printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
while (option != OFF) {
printf("Option: ");
option = get_option();
if (option == F_TO_C || option == C_TO_F) {
printf("Temp: ");
input_temp = get_input_temp();
}
switch (option) {
case F_TO_C:
printf("Celsius: %f\n", F_to_C(input_temp));
break;
case C_TO_F:
printf("Fahrenheit: %f\n", C_to_F(input_temp));
break;
case OFF:
puts("OFF"); // No need for printf()!
break;
default:
puts("Incorrect input, try again"); // No need for printf() as well!
break;
}
}
getchar();
}Fourthly, you should put function prototypes, enumerations and other objects that carry data (Of course, prototypes do not carry data, yet they belong in header files) into a separate header file (For instance,
main.h). That way your source file looks a lot cleaner, and if someone wants to use these functions elsewhere, he can easily find this header file with needed prototypes within.Fifthly, I'd consider either merging or getting rid of
get_option() and get_input_temp() functions as they perform the same task, just with different data types. I suggest to create a single function called get_input() that returns a float (Since float can be compared to int without requiring you worry about loosing precision) instead if you don't want to put them inside main() as we did with other redundant functions before. You may then compare these variables normally as usual arithmetic conversions from int to float will occur when comparing float to int (You can learn more about such conversions here, here and here).```
#include "main.h" // Prototypes and enum are there!
float F_to_C(float input_temp)
{
return (5.0 / 9.0) * (input_temp - 32);
}
float C_to_F(float input_temp)
{
return (9.0 / 5.0) * (input_temp)+32;
}
float get_input(void)
{
float input = 0;
scanf_s("%f", &input);
return input;
}
int main()
{
int option = -1;
float input_temp = 0.0;
printf("Temperature conversion Calculator!!\nPlease select an option:");
printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
while (option != OFF) {
printf("Option: ");
option = get_input();
Code Snippets
switch (option) {
case F_TO_C:
printf("Celsius: %f\n", F_to_C(input_temp));
break;
case C_TO_F:
printf("Fahrenheit: %f\n", C_to_F(input_temp));
break;
case OFF:
printf("OFF\n");
break;
default:
printf("Incorrect input, try again\n");
break;
}switch (option) {
case F_TO_C:
printf("Celsius: %f\n", F_to_C(input_temp));
break;
case C_TO_F:
printf("Fahrenheit: %f\n", C_to_F(input_temp));
break;
case OFF:
puts("OFF"); // No need for printf()!
break;
default:
puts("Incorrect input, try again"); // No need for printf() as well!
break;
}float F_to_C(float input_temp)
{
return (5.0 / 9.0) * (input_temp - 32);
}
float C_to_F(float input_temp)
{
return (9.0 / 5.0) * (input_temp)+32;
}
float get_input_temp()
{
float input_temp = 0;
scanf_s("%f", &input_temp);
return input_temp;
}
int get_option()
{
int option;
scanf_s("%d", &option);
return option;
}
int main()
{
int option = -1;
float input_temp = 0.0;
printf("Temperature conversion Calculator!!\nPlease select an option:");
printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
while (option != OFF) {
printf("Option: ");
option = get_option();
if (option == F_TO_C || option == C_TO_F) {
printf("Temp: ");
input_temp = get_input_temp();
}
switch (option) {
case F_TO_C:
printf("Celsius: %f\n", F_to_C(input_temp));
break;
case C_TO_F:
printf("Fahrenheit: %f\n", C_to_F(input_temp));
break;
case OFF:
puts("OFF"); // No need for printf()!
break;
default:
puts("Incorrect input, try again"); // No need for printf() as well!
break;
}
}
getchar();
}#include "main.h" // Prototypes and enum are there!
float F_to_C(float input_temp)
{
return (5.0 / 9.0) * (input_temp - 32);
}
float C_to_F(float input_temp)
{
return (9.0 / 5.0) * (input_temp)+32;
}
float get_input(void)
{
float input = 0;
scanf_s("%f", &input);
return input;
}
int main()
{
int option = -1;
float input_temp = 0.0;
printf("Temperature conversion Calculator!!\nPlease select an option:");
printf("\n1.) F to C\t2.) C to F\t3.) off\n\n");
while (option != OFF) {
printf("Option: ");
option = get_input();
if (option == F_TO_C || option == C_TO_F) {
printf("Temp: ");
input_temp = get_input();
}
switch (option) {
case F_TO_C:
printf("Celsius: %f\n", F_to_C(input_temp));
break;
case C_TO_F:
printf("Fahrenheit: %f\n", C_to_F(input_temp));
break;
case OFF:
puts("OFF"); // No need for printf()!
break;
default:
puts("Incorrect input, try again"); // No need for printf() as well!
break;
}
}
getchar();
}Context
StackExchange Code Review Q#139904, answer score: 7
Revisions (0)
No revisions yet.