Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Game looping problem?

Reply
Thread Tools

Game looping problem?

 
 
Mike
Guest
Posts: n/a
 
      11-02-2004
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;
}
 
Reply With Quote
 
 
 
 
Karthik Kumar
Guest
Posts: n/a
 
      11-02-2004
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. '
 
Reply With Quote
 
 
 
 
Mike
Guest
Posts: n/a
 
      11-02-2004
Karthik Kumar <(E-Mail Removed)> 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?
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      11-02-2004


Mike wrote:
> Karthik Kumar <(E-Mail Removed)> 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.

 
Reply With Quote
 
jemmaq99
Guest
Posts: n/a
 
      11-02-2004
On 2 Nov 2004 09:05:34 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (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.
 
Reply With Quote
 
Stuart Gerchick
Guest
Posts: n/a
 
      11-03-2004
(E-Mail Removed) (Mike) wrote in message news:<(E-Mail Removed). com>...
> 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?
 
Reply With Quote
 
xarax
Guest
Posts: n/a
 
      11-03-2004
"Stuart Gerchick" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> (E-Mail Removed) (Mike) wrote in message

news:<(E-Mail Removed). com>...
> > 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
looping in array vs looping in a dic giuseppe.amatulli@gmail.com Python 5 09-20-2012 11:58 PM
Game programming for kids: looking for open source 2D game developmentkit Max Kubierschky Python 10 03-31-2007 07:18 PM
I need help with card game game of war in GUI enviroment judith Java 0 11-01-2006 06:11 AM
Java Game- Game can't find it's graphics jar file BlackHawke Java 12 01-26-2004 06:14 AM



Advertisments