Hi, I've been working my way through "The C Programming Language" for about two days now, and am experimenting with some of the examples. Anyway, I'm at a point with what I call my 'Temperature Converter' where the app runs fine, except for the bolded part down below. You see, after I've entered '1' for Singly, and '1' or '2' for the next step, I input an integer, hit return, and have to do it once more to get a result. I don't understand why it's doing this. Please help, if you've got a moment; I can't figure it out.
#include<stdio.h>
int main()
{
int choice;
printf("\n\nWelcome to the Temperature Converter\n");
printf("\nWould you like to convert singly or incrementally?\n");
printf("1. Singly\t2. Incrementally ");
scanf("%d", &choice);
if (choice == 1) {
int choicetwo;
printf("\nIn which direction would you like to convert?\n");
printf("1. Celsius => Fahrenheit\t2. Fahrenheit => Celsius ");
scanf("%d", &choicetwo);
if (choicetwo == 1) {
int fahrenheit, celsius;
printf("\nPlease enter a starting value: ");
scanf("%d\n", &celsius);
fahrenheit = ((celsius * 9) / 5) + 32;
printf("Your corresponding fahrenheit value: %d", fahrenheit);
getchar();
getchar();
printf("\n");
return 0;
}
else if (choicetwo == 2) {
int fahrenheit, celsius;
printf("\nPlease enter a starting value: ");
scanf("%d\n", &fahrenheit);
celsius = 5 * (fahrenheit - 32) / 9;
printf("Your corresponding celsius value: %d", celsius);
getchar();
getchar();
printf("\n");
return 0;
}
}
else if (choice == 2) {
int fahrenheit, celsius;
int lower, upper, step;
printf("\nPlease enter a lower limit: ");
scanf("%d", &lower);
printf("Please enter an upper limit: ");
scanf("%d", &upper);
printf("Please enter a step size: ");
scanf("%d", &step);
while (fahrenheit <= upper) {
celsius = 5 * (fahrenheit - 32) / 9;
printf("%d\t%d\n", fahrenheit, celsius);
fahrenheit = fahrenheit + step;
}
getchar();
getchar();
printf("\n");
return 0;
}
}
#include<stdio.h>
int main()
{
int choice;
printf("\n\nWelcome to the Temperature Converter\n");
printf("\nWould you like to convert singly or incrementally?\n");
printf("1. Singly\t2. Incrementally ");
scanf("%d", &choice);
if (choice == 1) {
int choicetwo;
printf("\nIn which direction would you like to convert?\n");
printf("1. Celsius => Fahrenheit\t2. Fahrenheit => Celsius ");
scanf("%d", &choicetwo);
if (choicetwo == 1) {
int fahrenheit, celsius;
printf("\nPlease enter a starting value: ");
scanf("%d\n", &celsius);
fahrenheit = ((celsius * 9) / 5) + 32;
printf("Your corresponding fahrenheit value: %d", fahrenheit);
getchar();
getchar();
printf("\n");
return 0;
}
else if (choicetwo == 2) {
int fahrenheit, celsius;
printf("\nPlease enter a starting value: ");
scanf("%d\n", &fahrenheit);
celsius = 5 * (fahrenheit - 32) / 9;
printf("Your corresponding celsius value: %d", celsius);
getchar();
getchar();
printf("\n");
return 0;
}
}
else if (choice == 2) {
int fahrenheit, celsius;
int lower, upper, step;
printf("\nPlease enter a lower limit: ");
scanf("%d", &lower);
printf("Please enter an upper limit: ");
scanf("%d", &upper);
printf("Please enter a step size: ");
scanf("%d", &step);
while (fahrenheit <= upper) {
celsius = 5 * (fahrenheit - 32) / 9;
printf("%d\t%d\n", fahrenheit, celsius);
fahrenheit = fahrenheit + step;
}
getchar();
getchar();
printf("\n");
return 0;
}
}