473,396 Members | 1,990 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Game looping problem?

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;
}
Nov 14 '05 #1
6 1815
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. '
Nov 14 '05 #2
Karthik Kumar <ka*******************@yahoo.com> 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?
Nov 14 '05 #3


Mike wrote:
Karthik Kumar <ka*******************@yahoo.com> 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.

Nov 14 '05 #4
On 2 Nov 2004 09:05:34 -0800, az****@gmail.com (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.
Nov 14 '05 #5
az****@gmail.com (Mike) wrote in message news:<78**************************@posting.google. 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?
Nov 14 '05 #6
"Stuart Gerchick" <sg*******@bloomberg.net> wrote in message
news:e2**************************@posting.google.c om...
az****@gmail.com (Mike) wrote in message

news:<78**************************@posting.google. 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.
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: BlackHawke | last post by:
My name is Nick Soutter, I own a small game development company (www.aepoxgames.net) making our first game (www.andromedaonline.net) in java. I am writing because we are having a very...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
20
by: NeedJavaHelp | last post by:
Hello everyone, first time poster here, bear with me. RuneScape is an online multiplayer game run by Java www.runescape.com. The game itself is run on the website and when you play the game for...
5
by: Kraken | last post by:
Hi, i have a bit of a problem here. I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file...
19
by: foolsmart2005 | last post by:
I have written a snake game. There are 2 levels in the game(I finished 1st level). It can run in VC++ without problem but, when I run it on the dev C++ 4.9.9.2, it cannot run. I want to...
2
by: LilMeechc20 | last post by:
Hello, I have a group assignment that I have to do. We have to write a Tic Tac Toe game. One person in my group has managed to write the code for a multiplayer (human -vs- human) game and I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.