Thread: Number Guessing Game Help?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    26

    Number Guessing Game Help?

    First, thank you for even thinking of helping me. I am a n00b at C and trying to learn every bit I can. I am seeking help so I can understand for future projects, not just an answer Also excuse my poor English.

    I have a lot written in the program so far. I am able to loop the code, allow user to guess number and it seems to work. My questions are 1. How can I store how many guesses they guessed? I know I would store it in a variable and display it at some point, just not sure how to save it. 2. How can I end the loop when the user guesses the correct answer? My code is below. Again thank you for any help.




    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    
    int main(void)
    {
    	int x = 10;
    	int i = 0;
    	int target, guess;
    
    	/*create a random number*/
    	//create random function
    	srand(time(NULL));//this creates new number based on time which changes every second :)
    	target = rand() % 99; //create a random number using the rand() function, from 0 -99
    	
    	
    	/
    	do{
    		//increase the loop until it meets the x variable
    		i++;
    		
    		//allow user to input a number for guess
    		scanf("%d", &guess);
    		if (guess == target)
    		{
    			printf("You win! \n\n");
    			
    		}
    		else if (guess > target)
    		{
    			printf("You are too high. Guess a number:\n\n");
    		}
    	    else if (guess < target)
    		{
    			printf("You are too low. Guess a number:\n\n");
    		}
    		
    	}while(i < x);
    		printf("You lose, the number was %d \n\n", target);
    
    	
    	printf("Enter any key to exit...");
    	getchar();
    	getchar();
    	
    	return 0;
    
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacharyrs View Post
    My questions are 1. How can I store how many guesses they guessed?
    You already did -- that number is "i". Right?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    26
    Quote Originally Posted by MK27 View Post
    You already did -- that number is "i". Right?
    That is in place for the loop

    Code:
    //increase the loop until it meets the x variable
    		i++;

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    26
    Would I create an int variable, then increase each time in the loop?

    Code:
       int numGuessed=0;
    and later in the loop just do

    numGuessed++
    That way it gets added each time it loops?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacharyrs View Post
    That is in place for the loop
    Yes. And each iteration of the loop == one guess. Right?

    Quote Originally Posted by zacharyrs View Post
    Would I create an int variable, then increase each time in the loop?
    You can, but that number will be exactly the same as "i", is what I was trying to point out.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    26
    I was able to set a variable and increase it each time to show the number of tries. Yay for me. Anyone know best way of stopping the loop when the user has guessed the number, thus ending the loop before it has reached 10? Any help would be greatly appreciated.


    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<time.h>
    
    int main(void)
    {
    	int x = 10;
    	int i = 0;
    	int target, guess;
    	int numGuess = 0;
    
    	/*create a random number*/
    	//create random function
    	srand(time(NULL));//this creates new number based on time which changes every second :)
    	target = rand() % 99; //create a random number using the rand() function, from 0 -99
    	
    	
    	
    	do{
    		//increase the loop until it meets the x variable
    		i++;
    		numGuess++;
    		//allow user to input a number for guess
    		scanf("%d", &guess);
    		if (guess == target)
    		{
    			printf("You win! \n\n");
    			
    		}
    		else if (guess > target)
    		{
    			printf("You are too high. Guess a number:\n\n");
    		}
    	    else if (guess < target)
    		{
    			printf("You are too low. Guess a number:\n\n");
    		}
    		
    	}while(i < x);
    		printf("You lose, the number was %d. \n", target);
    
    	printf("Number of tries %d\n", numGuess);
    	printf("Enter any key to exit...");
    	getchar();
    	getchar();
    	
    	return 0;
    
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    break;

    They typically cover this in your book at the same time they cover loops.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    26
    Thank you for your help. Where would proper placement of the break be - outside loop or in the while?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Go read your book's section (or any tutorial) on loops again.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zacharyrs View Post
    Thank you for your help. Where would proper placement of the break be - outside loop or in the while?
    Obviously a break outside the loop couldn't possibly help.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    #include<time.h>
    
    int main(void)
    {
    	int x = 10;
    	int i = 0;
    	int target, guess;
    	int numGuess = 0;
    
    	/*create a random number*/
    	//create random function
    	srand(time(NULL));//this creates new number based on time which changes every second :)
    	target = rand() % 99; //create a random number using the rand() function, from 0 -99
    	
    	
    	
    	do{
    		//increase the loop until it meets the x variable
    		i++;
    		numGuess++;
    		//allow user to input a number for guess
    		scanf("%d", &guess);
    		if (guess == target)
    		{
    			printf("You win! \n\n");
    			break;//  comes out of loop
    		}
    		else if (guess > target)
    		{
    			printf("You are too high. Guess a number:\n\n");
    		}
    	    else if (guess < target)
    		{
    			printf("You are too low. Guess a number:\n\n");
    		}
    		
    	}while(i < x);
    		printf("You lose, the number was %d. \n", target);
    
    	printf("Number of tries %d\n", numGuess);
    	//printf("Enter any key to exit...");
    	//getchar();
    	//getchar();
    	
    	return 0;
    
    }

    why are you using getchar(); ? no need of it i think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Random guessing game
    By Nalif in forum C Programming
    Replies: 16
    Last Post: 10-26-2006, 03:05 AM
  4. Number guessing.
    By Lunatic Magnet in forum C Programming
    Replies: 5
    Last Post: 04-07-2006, 12:43 AM
  5. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM