Like Tree4Likes
  • 1 Post By Elysia
  • 3 Post By whiteflags

how do i program a guessing game?

This is a discussion on how do i program a guessing game? within the C++ Programming forums, part of the General Programming Boards category; Hye guys am new to programming. i rely need to noe how do i code the following requirements: Each digit ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    10

    how do i program a guessing game?

    Hye guys am new to programming. i rely need to noe how do i code the following requirements:


    • Each digit of the number cannot be repeated
    • The first digit can start with 0
    • If the number entered by the user has repeating digits, the user has to be asked to re-enter a new number
    • Each attempt of guessing need to be numbered
    • The user will be told how many digits correctly positioned and how many digits wrongly positioned after each guess
    • The user will be congratulated if the user successfully guessed the number.
    • The user will be asked whether to replay or not at the end of the game

    Plz help me

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Firstly, you need to know the basics of the language. Return with your attempt and we will resume...
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
    {
        int    guess;
        int play_again;
        int r_num;
        srand (time (NULL));
        r_num = (999-100)+1;
        
        play_again=0;
        
        do
        {
        printf("Hey am thinkin of a number!! Wanna guess?? ;) \n");
        scanf("%d\n", &guess);
        
        guess=0;
        do
        {
            if (guess != r_num)
        {
            printf("Wrong answer!!\n");
        }
    
        else
        {
            printf("Correct answer!!\n");
        }
    
        guess++;
        }
        while (r_num != guess);
        
        printf("The correct answer is %d\n", rand () % r_num + 100);
        printf("Do you wish to play again?? (Y/N)");
        scanf("%c", &play_again);
    
        play_again++;
        }
        while (play_again== 'y'||play_again=='Y');
        
        return 0;
    }
    
    this is my attempt... :)

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,625
    Ok, you can do a much better job on indentation but putting that aside, your code is almost complete. Just remember that in order to get a random number in the [LOW, HIGH] range, you do:
    Code:
    r_num = rand() % (HIGH - LOW + 1) + LOW;
    Devoted my life to programming...

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    i added some comments and tidied up your indentation
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main (void)
    {
        int    guess;					// init to 0
        int play_again;					// set this as char !!!!!!
        int r_num;
        srand (time (NULL));
        r_num = (999-100)+1;			// 100 is probably too big, your user would get very bored. - try 10!
        
        play_again=0;				//this should be a char and initialised to 'n' or some meaningful value
    								
        do
        {
    		printf("Hey am thinkin of a number!! Wanna guess?? ;) \n");
    		scanf("%d\n", &guess);
        
    		guess=0;							//pointless, you have just asked the user for a value and then scrubbed it out with a 0 immediately
    		do
    		{
    			if (guess != r_num)
    			{
    				printf("Wrong answer!!\n");
    			}
    			else
    			{
    				printf("Correct answer!!\n");		// move this outside this loop, else statement not neccesary - that would be my preference
    			}
    
    			guess++;					// why this ??? - has no meaning in the context, is not required, plus it is supposed to hold your user input.
    		}
    		while (r_num != guess);
        
    		printf("The correct answer is %d\n", rand () % r_num + 100);			//why dont you output guess here? since the user got it right?
    		printf("Do you wish to play again?? (Y/N)");
    		scanf("%c", &play_again);						//here you are asking the user for a value, then immediately making it 0 below ??????
    
    		play_again++;									//this makes no sense either - it is supposed to be your user inut char and should not be touched
        }													// until the condition in while loop can evaluate it
        while (play_again== 'y'||play_again=='Y');
        
        return 0;
    }
    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'"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,001
    Know the difference between C and C++. This belongs in the C section.
    Rodaxoleaux likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,759
    Quote Originally Posted by rogster001
    // 100 is probably too big, your user would get very bored. - try 10!
    Try binary search the next time someone plays this game with you. It doesn't matter how big the search space is.
    GReaper, rogster001 and Elysia like this.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,438
    haha i swear it was 100 first!
    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'"

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    Code:
     thank u so much guys!! i will work more on it come bk wit a btr coding :)

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main (void)
    {
    	int play_again;
    	int	guess;
    	int result[3];
    	int i;
    	int r_num;
    	srand (time (NULL));		
    	r_num = 9 + 1;
    		
    	play_again=0;
    	for (i = 0; i < 3; i++)
    {
    	result[i] = rand( ) % 3 + 1;
    	
    }
    	do
    	{
    	guess=0;
    	do
    	{
    	printf("Hey am thinkin of a number!! Wanna guess?? ;) \n");
    	scanf("%d", &guess);
    
    
    	if (guess != r_num)
    	{
    		printf("Wrong answer!!\n");
    	}
    
    
    	else
    	{
    		printf("Correct answer!!\n");
    	}
    
    
    	guess++;
    	}
    	while (r_num != guess);
    	
    	printf("The correct answer is %d\n", rand () % r_num);
    	printf("Do you wish to play again?? (Y/N)");
    	scanf("%c", &play_again);
    	}
    	while (play_again== 'y'||play_again=='Y');
    	
    	return 0;
    }
    
    i manage to come up with this... wat i dun understand now is, how i make the program tell the user on how many digits correctly positioned and how many digits wrongly positioned after each guess... help?? any guidance on this? thank u... :)

  11. #11
    Registered User
    Join Date
    Mar 2012
    Posts
    10
    guys anyone can help me with this??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do i program a guessing game?
    By Gayathri in forum Windows Programming
    Replies: 3
    Last Post: 03-28-2012, 12:46 PM
  2. Guessing game: how to quit the game?
    By hzr in forum C Programming
    Replies: 5
    Last Post: 12-18-2008, 10:53 AM
  3. help with guessing game program
    By dc0n in forum C++ Programming
    Replies: 13
    Last Post: 02-07-2006, 08:15 PM
  4. guessing game
    By Inferno in forum C++ Programming
    Replies: 2
    Last Post: 09-22-2004, 06:37 PM
  5. need help with a guessing game program
    By davidnj732 in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2003, 11:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21