Hello,
I am trying to use a code about magic number. But it doesn't work as it is supposed to be. When I try to enter a new magic number by pressing 1, it doesn't wait for me to enter a number. It goes back to the do-while loop and prints the menu again.
Any help would be greatly appreciated,
Code:#include <stdio.h> #include <stdlib.h> int play(int m); int next_magic(); int getnum(); main() { int option; int magic; do{ printf("1. Define a new magic number\n"); printf("2. Play\n"); printf("3. Quit\n"); printf("Enter your choice:\n"); scanf("%d",&option); switch(option){ case 1: magic=next_magic(); break; case 2: play(magic); break; case 3: printf("Goodbye\n"); break; } }while(option!=3); } int next_magic() { printf("enter new magic number:"); return(getnum()); } int getnum() { char s[30]; gets(s); return(atoi(s)); } int play(int m) { int t; int x; for(t=0;t<10;t++){ printf("Guess the number"); x=getnum(); if(x==m){ printf("***Right***"); return; } else if(x<m) printf("Too Low\n"); else printf("Too High\n"); } printf("You used up all your guess.. try again"); }