# Switch statement problems

• 03-01-2008
BSmith4740
Switch statement problems
I am writing a program which incorprates a switch statement. I am having problem withs with the menu function.

say you are prompting the user to input some value. Let's call this this case one. Inside the menu function on the program how do you tell the computer to go get case one.

For instance I am trying.

printf("Input leg1:",case 1);

This isnt working at all. Any ideas.
• 03-01-2008
laserlight
You read the value into a variable, then use the switch on the variable.
• 03-01-2008
BSmith4740
Code:

```float leg1 = -1.0, leg2 = -1.0, hypotenuse = -1.0, area = 0;     case '1' :  44                        {  45                                printf("Enter leg1: ");  46                                scanf("&#37;f", &leg1);  47                                while(leg1 <= 0)  48                                {  49                                        printf("Please enter a positive number: ");  50                                        scanf("%f", &leg1);  51                                }  52                        }  53                        break; Menu Printing Function; 219 void printMenu() 220 221 { 222 223 224  printf("Input value for leg1:",case '1'); 225  printf("Input value for leg2:"); 226  printf("Input value for hypotenuse:"); 227  printf("Calculate leg1:"); 228  printf("Calculate leg2:"); 229  printf("Calculate hypotenuse:"); 230  printf("Calculate area of right triangle:"); 214 } 215```
What do you mean by that?
• 03-01-2008
BSmith4740
In C please? I don't understand what you said.
• 03-01-2008
vart
Code:

```int input; printf("Enter your choice (0 - exit, 1 - do something usefull):"); flush(stdout); if( scanf("&#37;d",&input) == 1) {   switch(input)   {       case 0: return 0;       case 1:         /* do something useful */         break;       default:         /* wrong input - do something stupid */   } } else {   /* wrong input - clear the stdin and try again */ }```
• 03-01-2008
dwks
Code:

`flush(stdout);`
fflush(), of course.

BTW -- "usefull" has one 'L' . . . .

Code:

`/* wrong input - clear the stdin and try again */`
Here's one way to do that:
Code:

`while(getchar() != '\n');`