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 :)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

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",&degrees);
                              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.