0

I do not understand why my program runs correctly and then when the do...while repeats it enters my else function when I do not want it to. After it does this my program then repeats like I want it to. I am sorry for the poor explanation but I think the picture below "My program" shows better than I can explain what is happening.

Execution image

#include <stdio.h>
#include <stdlib.h>

int main()
{
float fahrenheit,celsius;
char selection[30];
char *idontknow;
long trueselection;

do{
printf("1: Fahrenheit to Celsius."); 
printf("\n2: Celsius to Fahrenheit.");
printf("\n3: Quit program.\n");
fgets(selection,10,stdin);  //This line and the line below are in the program 
//so that if the user inputs a character instead of an integer it does not infinite loop. 
trueselection = strtol(selection, &idontknow, 10);

if(trueselection==1){
    printf("Enter temperature in Fahrenheit: ");
    scanf("%f",&fahrenheit);
    celsius= (fahrenheit - 32) / 1.8;
    printf("Temperature in Celsius: %.2f\n\n\n",celsius);
}
else if(trueselection==2){
    printf("Enter temperature in Celsius: ");
    scanf("%f",&celsius);
    fahrenheit= (celsius*1.8)+32;
    printf("Temperature in Fahrenheit: %.2f\n\n\n",fahrenheit);
}
else if(trueselection==3){ //This "else if" statement is so the program does not say invalid selection before exiting if 3 is entered. 
return 0;
}
else{
    printf("Invalid selection !!!\n\n\n");
}
}
while(trueselection!=3); //This line tells the program to repeat back to             line 3 if any selection but 3 is selected. 
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
RyanK
  • 1
  • I'm sorry I am still having a little bit of a hard time understanding. So the fgets() is reading the \n from my if statements? – RyanK Sep 16 '17 at 03:09
  • Nvm, thank you BLUEPIXY this is my third week since the first time I ever programmed any computer language and I have a hard time understanding the lingo on this site, but after re-reading the duplicate link you gave several times I finally understood that I could not mix scanf() and fgets(). – RyanK Sep 16 '17 at 03:38

1 Answers1

0
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float fahrenheit,celsius;
    int selection;
    char *idontknow;
    long trueselection;

    do{
        printf("1: Fahrenheit to Celsius."); 
        printf("\n2: Celsius to Fahrenheit.");
        printf("\n3: Quit program.\n");
        scanf("%d",&selection);

        if(trueselection==1){
            printf("Enter temperature in Fahrenheit: ");
            scanf("%f",&fahrenheit);
            celsius= (fahrenheit - 32) / 1.8;
            printf("Temperature in Celsius: %.2f\n\n\n",celsius);
        }
        else if(trueselection==2){
            printf("Enter temperature in Celsius: ");
            scanf("%f",&celsius);
            fahrenheit= (celsius*1.8)+32;
            printf("Temperature in Fahrenheit: %.2f\n\n\n",fahrenheit);
        }
        else if(trueselection==3){ //This "else if" statement is so the program does not say invalid selection before exiting if 3 is entered. 
            break;
        }
        else{
            printf("Invalid selection !!!\n\n\n");
        }
    }
    while(trueselection != 3); //This line tells the program to repeat back to line 3 if any selection but 3 is selected. 
}
Dr. X
  • 2,890
  • 2
  • 15
  • 37