Game looping problem?

Discussion in 'C Programming' started by Mike, Nov 2, 2004.

  1. Mike

    Mike Guest

    Hello,

    This game will not loop. It compiles, builds, and executes fine, but
    the game will not loop around no matter what I do. What is wrong with
    the code? What do I have to add to make it loop till the person
    selects "N" or "n".


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

    #define RANGE 10
    #define TRIES 1

    int rnd(int range);
    void seedrnd(void);

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

    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
    printf("Too high!\n");
    }
    printf("The answer was %i.\n",guessme);
    getchar();
    }

    int rnd(int range)
    {
    int r;

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

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

    int playAgain(void)
    {
    char answer;
    printf("Do you want to play again? Enter Y or y (yes) and N or n (no):
    ");
    scanf("%c",&answer);
    if (answer == 'Y' || answer == 'y')
    {
    return 1;
    }
    else if (answer == 'N' || answer == 'n')
    {
    printf("\n");
    printf("Good Bye! Have A Nice Day!!!\n");
    return 0;
    }
    else
    {
    printf("Do you want to play again? Enter Y or y (yes) and N or n (no):
    ");
    scanf("%c",&answer);
    }
    return answer;
    }
    Mike, Nov 2, 2004
    #1
    1. Advertising

  2. Mike wrote:
    > Hello,
    >
    > This game will not loop. It compiles, builds, and executes fine, but
    > the game will not loop around no matter what I do. What is wrong with
    > the code? What do I have to add to make it loop till the person
    > selects "N" or "n".
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > #define RANGE 10
    > #define TRIES 1


    TRIES set to 1
    >
    > int rnd(int range);
    > void seedrnd(void);
    >
    > void main()
    > {
    > int guessme,guess,t;
    >
    > 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++)


    You loop from 1 to TRIES, ( which was set to 1 ),
    Why would you expect this to loop ?

    > {
    > 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
    > printf("Too high!\n");
    > }
    > printf("The answer was %i.\n",guessme);
    > getchar();
    > }
    >
    > int rnd(int range)
    > {
    > int r;
    >
    > r=rand()%range;
    > return(r);
    > }
    >
    > void seedrnd(void)
    > {
    > srand((unsigned)time(NULL));
    > }
    >


    This function playAgain is not invoked anywhere.
    Please post the precise code that you were working with.

    > int playAgain(void)
    > {


    [code snipped]

    --
    Karthik. http://akktech.blogspot.com .
    ' Remove _nospamplz from my email to mail me. '
    Karthik Kumar, Nov 2, 2004
    #2
    1. Advertising

  3. Mike

    Mike Guest

    Karthik Kumar <> wrote in message news:<418703b0$1@darkstar>...
    > Mike wrote:
    > > Hello,
    > >
    > > This game will not loop. It compiles, builds, and executes fine, but
    > > the game will not loop around no matter what I do. What is wrong with
    > > the code? What do I have to add to make it loop till the person
    > > selects "N" or "n".
    > >
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <time.h>
    > >
    > > #define RANGE 10
    > > #define TRIES 1

    >
    > TRIES set to 1
    > >
    > > int rnd(int range);
    > > void seedrnd(void);
    > >
    > > void main()
    > > {
    > > int guessme,guess,t;
    > >
    > > 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++)

    >
    > You loop from 1 to TRIES, ( which was set to 1 ),
    > Why would you expect this to loop ?
    >
    > > {
    > > 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
    > > printf("Too high!\n");
    > > }
    > > printf("The answer was %i.\n",guessme);
    > > getchar();
    > > }
    > >
    > > int rnd(int range)
    > > {
    > > int r;
    > >
    > > r=rand()%range;
    > > return(r);
    > > }
    > >
    > > void seedrnd(void)
    > > {
    > > srand((unsigned)time(NULL));
    > > }
    > >

    >
    > This function playAgain is not invoked anywhere.
    > Please post the precise code that you were working with.
    >
    > > int playAgain(void)
    > > {

    >
    > [code snipped]


    > You loop from 1 to TRIES, ( which was set to 1 ),
    > Why would you expect this to loop ?


    The only reason that was set to 1 is to make it run faster for now.
    Later I will increase the number. The looping issue is with the Y or y
    or N or n. What would I need to ammend or change in order to make this
    run right?
    Mike, Nov 2, 2004
    #3
  4. Mike

    Michael Mair Guest

    Mike wrote:
    > Karthik Kumar <> wrote in message news:<418703b0$1@darkstar>...
    >
    >>Mike wrote:
    >>
    >>>Hello,
    >>>
    >>>This game will not loop. It compiles, builds, and executes fine, but
    >>>the game will not loop around no matter what I do. What is wrong with
    >>>the code? What do I have to add to make it loop till the person
    >>>selects "N" or "n".
    >>>
    >>>
    >>>#include <stdio.h>
    >>>#include <stdlib.h>
    >>>#include <time.h>
    >>>
    >>>#define RANGE 10
    >>>#define TRIES 1

    >>
    >> TRIES set to 1
    >>
    >>>int rnd(int range);
    >>>void seedrnd(void);
    >>>
    >>>void main()
    >>>{
    >>> int guessme,guess,t;
    >>>
    >>> 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++)

    >>
    >>You loop from 1 to TRIES, ( which was set to 1 ),
    >>Why would you expect this to loop ?
    >>
    >>
    >>> {
    >>> 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
    >>> printf("Too high!\n");
    >>> }
    >>> printf("The answer was %i.\n",guessme);
    >>> getchar();
    >>>}
    >>>
    >>>int rnd(int range)
    >>>{
    >>> int r;
    >>>
    >>> r=rand()%range;
    >>> return(r);
    >>>}
    >>>
    >>>void seedrnd(void)
    >>>{
    >>> srand((unsigned)time(NULL));
    >>>}
    >>>

    >>
    >>This function playAgain is not invoked anywhere.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>Please post the precise code that you were working with.

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>
    >>
    >>>int playAgain(void)
    >>>{

    >>
    >>[code snipped]

    >
    >
    >>You loop from 1 to TRIES, ( which was set to 1 ),
    >>Why would you expect this to loop ?

    >
    >
    > The only reason that was set to 1 is to make it run faster for now.
    > Later I will increase the number. The looping issue is with the Y or y
    > or N or n. What would I need to ammend or change in order to make this
    > run right?


    Why do you not _READ_ the answer he gave?
    I repeat: "playAgain is not invoked anywhere" -- so how should the
    loop ever be put to work?

    I suggest a do-while loop from before guessme=rnd(RANGE)+1; to the
    end of main, with ...} while(playAgain); at the end.

    If this is not the issue, comply to "Please post the precise code that
    you were working with."


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.
    Michael Mair, Nov 2, 2004
    #4
  5. Mike

    jemmaq99 Guest

    On 2 Nov 2004 09:05:34 -0800, (Mike) wrote:

    >The only reason that was set to 1 is to make it run faster for now.
    >Later I will increase the number. The looping issue is with the Y or y
    >or N or n. What would I need to ammend or change in order to make this
    >run right?


    Mike, your program needs two loops.

    The inner loop will control the the number of tries the
    user gets to guess.

    The outer loop will control whether the user wants to
    play again.

    While there are a number of ways you could do it, a
    do...while() construct is a good candidate for your
    outer loop with your existing for() code as the inner
    loop.

    --J.
    jemmaq99, Nov 2, 2004
    #5
  6. (Mike) wrote in message news:<>...
    > Hello,
    >
    > This game will not loop. It compiles, builds, and executes fine, but
    > the game will not loop around no matter what I do. What is wrong with
    > the code? What do I have to add to make it loop till the person
    > selects "N" or "n".
    >
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <time.h>
    >
    > #define RANGE 10
    > #define TRIES 1
    >
    > int rnd(int range);
    > void seedrnd(void);
    >
    > void main()
    > {
    > int guessme,guess,t;
    >
    > 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
    > printf("Too high!\n");
    > }
    > printf("The answer was %i.\n",guessme);
    > getchar();
    > }
    >
    > int rnd(int range)
    > {
    > int r;
    >
    > r=rand()%range;
    > return(r);
    > }
    >
    > void seedrnd(void)
    > {
    > srand((unsigned)time(NULL));
    > }
    >
    > int playAgain(void)
    > {
    > char answer;
    > printf("Do you want to play again? Enter Y or y (yes) and N or n (no):
    > ");
    > scanf("%c",&answer);
    > if (answer == 'Y' || answer == 'y')
    > {
    > return 1;
    > }
    > else if (answer == 'N' || answer == 'n')
    > {
    > printf("\n");
    > printf("Good Bye! Have A Nice Day!!!\n");
    > return 0;
    > }
    > else
    > {
    > printf("Do you want to play again? Enter Y or y (yes) and N or n (no):
    > ");
    > scanf("%c",&answer);
    > }
    > return answer;
    > }


    Maybe I am stupid, but I do not see where playAgain is called. Isnt
    this where thr issue you are refering to would be?
    Stuart Gerchick, Nov 3, 2004
    #6
  7. Mike

    xarax Guest

    "Stuart Gerchick" <> wrote in message
    news:...
    > (Mike) wrote in message

    news:<>...
    > > Hello,
    > >
    > > This game will not loop. It compiles, builds, and executes fine, but
    > > the game will not loop around no matter what I do. What is wrong with
    > > the code? What do I have to add to make it loop till the person
    > > selects "N" or "n".
    > >
    > >
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <time.h>
    > >
    > > #define RANGE 10
    > > #define TRIES 1
    > >
    > > int rnd(int range);
    > > void seedrnd(void);
    > >
    > > void main()
    > > {
    > > int guessme,guess,t;
    > >
    > > 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
    > > printf("Too high!\n");
    > > }
    > > printf("The answer was %i.\n",guessme);
    > > getchar();
    > > }
    > >
    > > int rnd(int range)
    > > {
    > > int r;
    > >
    > > r=rand()%range;
    > > return(r);
    > > }
    > >
    > > void seedrnd(void)
    > > {
    > > srand((unsigned)time(NULL));
    > > }
    > >
    > > int playAgain(void)
    > > {
    > > char answer;
    > > printf("Do you want to play again? Enter Y or y (yes) and N or n (no):
    > > ");
    > > scanf("%c",&answer);
    > > if (answer == 'Y' || answer == 'y')
    > > {
    > > return 1;
    > > }
    > > else if (answer == 'N' || answer == 'n')
    > > {
    > > printf("\n");
    > > printf("Good Bye! Have A Nice Day!!!\n");
    > > return 0;
    > > }
    > > else
    > > {
    > > printf("Do you want to play again? Enter Y or y (yes) and N or n (no):
    > > ");
    > > scanf("%c",&answer);
    > > }
    > > return answer;
    > > }

    >
    > Maybe I am stupid, but I do not see where playAgain is called. Isnt
    > this where thr issue you are refering to would be?


    No. Look at the for-loop limit TRIES.
    xarax, Nov 3, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. BlackHawke
    Replies:
    12
    Views:
    1,565
    Andrew Thompson
    Jan 26, 2004
  2. judith
    Replies:
    0
    Views:
    1,647
    judith
    Nov 1, 2006
  3. Max Kubierschky
    Replies:
    10
    Views:
    1,839
    pabloreda
    Mar 31, 2007
  4. Advertiser for `2D Games Development Central`

    {Game Development} 2D Game Development Central

    Advertiser for `2D Games Development Central`, May 7, 2008, in forum: Java
    Replies:
    2
    Views:
    411
    RedGrittyBrick
    May 8, 2008
  5. Replies:
    5
    Views:
    271
Loading...

Share This Page