Game looping problem?

M

Mike

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;
}
 
K

Karthik Kumar

Mike said:
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]
 
M

Mike

Karthik Kumar said:
Mike said:
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?
 
M

Michael Mair

Mike said:
Karthik Kumar said:
Mike said:
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
 
J

jemmaq99

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.
 
S

Stuart Gerchick

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?
 
X

xarax

Stuart Gerchick said:
(e-mail address removed) (Mike) wrote in message

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top