I made a simple code that converts Celsius to Fahrenheit and vice versa, but for some reason, whenever I make an input other than 'C' or 'F' for the unit, it gives me 2 error messages instead of one. How do I get rid of this??
Here's the code:
int main() {
printf("Temperature Unit Converter\n\n");
start:
char unit;
double temp;
printf("Enter unit of measurement (C or F): ");
scanf("%c", &unit);
unit = toupper(unit);
if (unit == 'C') {
printf("\nEnter temperature in %c: ", unit);
scanf("%lf", &temp);
temp = (temp * 9/5) + 32;
printf("\nThe temperature = %.1lf degrees Fahrenheit", temp);
}
else if (unit == 'F') {
printf("\nEnter temperature in %c: ", unit);
scanf("%lf", &temp);
temp = (temp - 32) * 5/9;
printf("\nThe temperature = %.1lf degrees Celsius", temp);
}
else {
printf("\nYou have entered an invalid unit. Please try again.\n");
goto start;
}
return 0;
I also tried inputting the unit with getchar(), and using an else if (unit != 'C' && unit != 'F') instead, but they both gave me the same result.