### While loop problem

For this program I want the menu to loop at the end of a result of any case, however if they press 5 the program will exit.

What happens is the menu displays twice on the screen after the result and the default switch statement is always executed. Why is that?

All good solved :)

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162`` ``````#include #include #include #include int main() { float minutes; float degrees; float feet; float fahrenheit; int choice; //display menu options do{ printf("Please choose one of the following options:\n"); printf("\n\t\t1.Minutes to Hours"); printf("\n\t\t2.Degrees to Radians"); printf("\n\t\t3.Feet to Meters"); printf("\n\t\t4.Degrees Fahrenheit to degrees Celsius"); printf("\n\t\t5.Exit\n\t\t"); fflush(stdin); scanf("%d",&choice); switch(choice) { case 1: printf("\nPlease enter the number of minutes: "); scanf("%f",&minutes); printf("\nThe equivlent number of hours is: %.1f hr/s\n", minutes/60); break; case 2: printf("\nPlease enter a number in degrees: "); scanf("%f",°rees); printf("\nThe equivalent number of radians is: %f°\n",degrees*M_PI/180); break; case 3: printf("\nPlease enter a number in feet: "); scanf("%f",&feet); printf("\nThe equivalent number of meters is: %f\n",feet/3.2808); break; case 4: printf("\nPlease enter a number in degrees fahrenheit: "); scanf("%f",&fahrenheit); printf("\nThe equivalent number of degrees celsius is %.2f degrees\n",(fahrenheit-32)*5/9); break; case 5: printf("\n\t\tGoodbye\n"); break; default: printf("\nThis option does not exist\n"); break; } }while(choice!=5); system("pause"); } ``````
Last edited on
the problem is that `scanf("%f", ...);` leaves the end of line in the stream. The next iteration `getchar();` reads the end of line and the switch goes to the default
Thanks,
I've updated the code. So I have got it working now however if you read what's in the description I am having some trouble with double digits.
Last edited on
Topic archived. No new replies allowed.