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 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();
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();
}
Also, be sure to check chux's answer. It contains even more useful tips and tricks to improve your code. Especially scanf_s()
checks, option = -1
change and math simplification.