Hey algorism, thanks so much for the help!
Here's where my code sits after editing and fixing that typo in my Celsius conversion:
Code:
#include <stdio.h>
float convert_KtoP();
float convert_LtoG();
float convert_CtoI();
float convert_CtoF();
int main(void){
char menuOption = 0;
while (menuOption != 5){
printf("Main menu: \n");
printf("1: Kilograms - Pounds \n");
printf("2: Litres - Gallons \n");
printf("3: Centimetres - Inches \n");
printf("4: Celsius - Fahrenheit \n");
printf("5: Exit program. \n");
printf("Please enter (1-5), corresponding to the options above. ");
if (scanf("%d", &menuOption) != 1) {
while (getchar() != '\n');
continue;
}
switch(menuOption){
case 1:
convert_KtoP();
break;
case 2:
convert_LtoG();
break;
case 3:
convert_CtoI();
break;
case 4:
convert_CtoF();
break;
case 5:
printf("Thank you for using my program!\n");
break;
default:
printf("Sorry, that is not a valid option.\n");
break;
}
}
return 0;
}
float convert_KtoP(){
char directionChar;
float initialMeasure, finalMeasure;
printf("Enter 'K' for kilograms to pounds, or 'P' for pounds to kilograms: ");
scanf(" %c", &directionChar);
if (directionChar == 'K'){
printf("Please enter the number of kilograms: ");
scanf("%f", &initialMeasure);
finalMeasure = initialMeasure * 2.2;
printf("The equivalent number of pounds is: %.2f\n\n", finalMeasure);
}
else if (directionChar == 'P'){
printf("Please enter the number of pounds: ");
scanf("%f", &initialMeasure);
finalMeasure = initialMeasure / 2.2;
printf("The equivalent number of kilograms is: %.2f\n\n", finalMeasure);
}
return 0;
}
float convert_LtoG(){
char directionChar;
float initialMeasure, finalMeasure;
printf("Enter 'L' for litres to gallons, or 'G' for gallons to litres: ");
scanf(" %c", &directionChar);
if (directionChar == 'L'){
printf("Please enter the number of litres: ");
scanf("%f", &initialMeasure);
finalMeasure = initialMeasure / 3.79;
printf("The equivalent number of gallons is: %.2f\n\n", finalMeasure);
}
else if (directionChar == 'G'){
printf("Please enter the number of gallons: ");
scanf("%f", &initialMeasure);
finalMeasure = initialMeasure * 3.79;
printf("The equivalent number of litres is: %.2f\n\n", finalMeasure);
}
}
float convert_CtoI(){
char directionChar;
float initialMeasure, finalMeasure;
printf("Enter 'C' for centimetres to inches, or 'I' for inches to centimetres: ");
scanf(" %c", &directionChar);
if (directionChar == 'C'){
printf("Please enter the number of centimetres: ");
scanf("%f", &initialMeasure);
finalMeasure = initialMeasure / 2.54;
printf("The equivalent number of inches is: %.2f\n\n", finalMeasure);
}
else if (directionChar == 'I'){
printf("Please enter the number of inches: ");
scanf("%f", &initialMeasure);
finalMeasure = initialMeasure * 2.54;
printf("The equivalent number of centimetres is: %.2f\n\n", finalMeasure);
}
}
float convert_CtoF(){
char directionChar;
float initialMeasure, finalMeasure;
printf("Enter 'C' for Celsius to Fahrenheit, or 'F' for Fahrenheit to Celsius: ");
scanf(" %c", &directionChar);
if (directionChar == 'C'){
printf("Please enter the degrees in Celsius: ");
scanf("%f", &initialMeasure);
finalMeasure = ((initialMeasure * 9) / 5) + 32;
printf("The equivalent temperature in Fahrenheit is: %.2f\n\n", finalMeasure);
}
else if (directionChar == 'F'){
printf("Please enter the degrees in Fahrenheit: ");
scanf("%f", &initialMeasure);
finalMeasure = ((initialMeasure - 32) * 5) / 9;
printf("The equivalent temperature in Celsius is: %.2f\n\n", finalMeasure);
}
}
Any other advice to make it more efficient is fine, though I think this is suitable for the purposes of this part of my assignment.
Thanks so much!