Thread: Need help for my Magic Number Program

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Unhappy Need help for my Magic Number Program

    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");
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    28
    Try writing getchar(); just before starting while loop

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Why would it wait? You have to put some code in to make it do that, why don't you just use scanf again in the next_magic() function?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Try call this function just after the scanf function call in main.

    Code:
    void flush( void )
    {
         int ch;
         while( ( ch = getchar() ) != '\n' && ch != EOF );
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect Number program
    By tiger6425 in forum C Programming
    Replies: 7
    Last Post: 09-03-2010, 05:17 PM
  2. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  3. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM