Need help

This is a discussion on Need help within the Game Programming forums, part of the General Programming Boards category; I am new to programming, and I just did this example out of my book. You pick the number, you ...

  1. #1
    Registered User slick1537's Avatar
    Join Date
    Dec 2001
    Posts
    9

    Need help

    I am new to programming, and I just did this example out of my book. You pick the number, you get 6 trys, if you pick the right one you win. Anywho I want to game to repeat itself if you press Y. I know how to make the game exit if you press N. Is there C function to make the program repeat the main function?
    Here is the scource code.

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    #define RANGE 100
    #define TRIES 6

    void main()
    {
    int guessme,guess,t,again;

    seedrnd();
    guessme=rnd(RANGE)+1;

    printf("GUESS!?!\nGuess the random number.\n");
    printf("I'm thinking of a number between 1 and %i.\n",RANGE);
    printf("Can you guess it in %i tries?\n",TRIES);

    for(t=1;t<=TRIES;t++)
    {
    printf("Guess #%i:",t);
    scanf("%i",&guess);

    if(guess==guessme)
    {
    printf("You got it!\n");
    break;
    }
    else if(guess<guessme)
    printf("Too low!\n");
    else if(guess>guessme)
    printf("Too high!\n");
    }

    printf("The answer was %i.\n",guessme);

    // printf("Would you like to play again? Y/N?");
    // again=getche();
    // if(again=='y');


    // else if getche(printf("\nPress any key to exit."));
    }

    int rnd(int range)
    {
    int r;

    r=rand()%range;
    return(r);
    }

    void seedrnd(void)
    {
    srand((unsigned)time(NULL));
    }

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    4
    There isn't a function for that (at least not one I know of). I would either put everything in a while loop, or use a label.

    For the label:
    {

    put a label name followed by a colon after seedrnd() and before you call rnd, like so:

    srnd();
    start:
    guessme = rnd(RANGE)+1;

    then your if statement would look like this:

    if ( again == 'y' )
    {
    goto start;
    }


    }

    Or you could do a while loop that starts right after the seedrnd() and ends where your if statement is now, and remember to initialize again.

    Like so:
    {

    again = 'y';

    seedrnd();
    while ( again == 'y' )
    {
    //etc...

    }

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    check this out.. this is one way of doing it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <time.h>
    
    #define RANGE 100
    #define TRIES 6
    
    int rnd(int range);
    void seedrnd(void);
    
    void main()
    {
    	int guessme, guess, t;
    	char again[4];
    
    	do
    	{
    		seedrnd();
    
    		guessme = rnd(RANGE) + 1;
    
    		printf("GUESS!?!\nGuess the random number.\n");
    		printf("I'm thinking of a number between 1 and %i.\n",RANGE);
    		printf("Can you guess it in %i tries?\n",TRIES);
    
    		for(t = 1; t <= TRIES; t++)
    		{
    			printf("Guess #%i:",t);
    
    			scanf("%i", &guess);
    			fflush(stdin);
    
    			if(guess == guessme)
    			{
    				printf("You got it!\n");
    				break;
    			}
    			else if(guess < guessme)
    			{
    				printf("Too low!\n");
    			}
    			else
    			{
    				printf("Too high!\n");
    			}
    		}
    
    		printf("The answer was %i.\n\n",guessme);
    
    		printf("Play again ? ");
    		fgets(again, sizeof(again), stdin);
    
    	} while(toupper(again[0]) == 'Y');
    }
    
    int rnd(int range)
    {
    	int r;
    
    	r = rand() % range;
    	return(r);
    }
    
    void seedrnd(void)
    {
    	srand((unsigned)time(NULL));
    }
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  4. #4
    Registered User slick1537's Avatar
    Join Date
    Dec 2001
    Posts
    9
    Well seeing as I don't really understand what you are saying, and I havent read the while loop part yet, I figured out what I could do. I will put most the stuff in the main function in a different function. Then I will make the main function a loop that points to the new funtion.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    bobthemighty: don't ever use goto statements! It's a big no-no.

    If you structure your code proplery you should never need them.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

Popular pages Recent additions subscribe to a feed

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