c - menu based programme withwhile and switch statements? -



c - menu based programme withwhile and switch statements? -

i beginner , trying larn on own. wrote switch case statement on own menu based one.all programme ok when menu reappears menu not appearing properly!

code is

int main() { char menu int i,num,fact=1,d; while(1) { printf("enter 'a' find out factorial of number\n"); printf("enter 'b' find out number prime or not\n"); printf("enter 'c' find out number odd or even\n"); printf("enter 'd' exit"); scanf("%c",&menu); switch(menu) { case 'a': printf("please come in number "); scanf("%d",&num); for(i=1; i<=num; i++) fact=fact*i; printf("factorial %d\n",fact); break; case'b': printf("enter number"); scanf("%d",&num); for(i=2; i<=num-1; i++) { if(num%i==0) { printf("not prime number\n"); break; } } if(i==num) { printf("prime number\n"); break; } break; case'c': printf("enter number\n"); scanf("%d",&num); d=num%2; if(d=0) printf("its number\n"); else printf("its odd one\n"); break; case'd': exit(0); } } }

try this:

int ok=1; while(1) { if(ok==1) { printf("enter 'a' find out factorial of number\n"); printf("enter 'b' find out number prime or not\n"); printf("enter 'c' find out number odd or even\n"); printf("enter 'd' exit\n"); scanf("%c",&menu); fflush(stdin); } ok=1; switch(menu) { case 'a': printf("please come in number "); scanf("%d",&num); for(i=1; i<=num; i++) fact=fact*i; printf("factorial %d\n",fact); break; ...... ...... default: ok=0;break; } }

the menu works fine on scheme modification.

also check out shekhar suman's reply remove obvious bugs.

c switch-statement

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -