473,796 Members | 2,826 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(RAN GE)+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<=TRIE S;t++)
{
printf("Guess #%i:",t);
scanf("%i",&gue ss);

if(guess==guess me)
{
printf("You got it!\n");
break;
}
else if(guess<guessm e)
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",&ans wer);
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",&ans wer);
}
return answer;
}
Nov 14 '05 #1
6 1836
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(RAN GE)+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<=TRIE S;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",&gue ss);

if(guess==guess me)
{
printf("You got it!\n");
break;
}
else if(guess<guessm e)
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.blogspo t.com .
' Remove _nospamplz from my email to mail me. '
Nov 14 '05 #2
Karthik Kumar <ka************ *******@yahoo.c om> 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(RAN GE)+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<=TRIE S;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",&gue ss);

if(guess==guess me)
{
printf("You got it!\n");
break;
}
else if(guess<guessm e)
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.c om> 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(RAN GE)+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<=TRIE S;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",&gue ss);

if(guess==guess me)
{
printf("You got it!\n");
break;
}
else if(guess<guessm e)
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(RAN GE)+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.co m (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.co m (Mike) wrote in message news:<78******* *************** ****@posting.go ogle.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(RAN GE)+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<=TRIE S;t++)
{
printf("Guess #%i:",t);
scanf("%i",&gue ss);

if(guess==guess me)
{
printf("You got it!\n");
break;
}
else if(guess<guessm e)
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",&ans wer);
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",&ans wer);
}
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*******@bloo mberg.net> wrote in message
news:e2******** *************** ***@posting.goo gle.com...
az****@gmail.co m (Mike) wrote in message

news:<78******* *************** ****@posting.go ogle.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(RAN GE)+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<=TRIE S;t++)
{
printf("Guess #%i:",t);
scanf("%i",&gue ss);

if(guess==guess me)
{
printf("You got it!\n");
break;
}
else if(guess<guessm e)
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",&ans wer);
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",&ans wer);
}
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
4456
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 serious problem, and I was hoping someone might have thoughts.
138
6605
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 yeah,-that's-non-obvious dept. theodp writes "As if there weren't enough dodgy patents, here's an excerpt from one granted to Microsoft Tuesday for a 'Method and apparatus for displaying information regarding stored data in a gaming system': 'When saving a game,...
7
3199
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
2856
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 found a bug that I can't figure out (believe me; I've tried everything and asked for help several times on the IRC-channel). The updateMsg() and iMsg() takes a list of message-lines as argument (or process a string to a list), and it'll output...
20
20368
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 the first time, its files store on your pc. I've never had a problem untill its makers switched to Java Se (SUN Java). Normally, when you move to a new location / area in this game it brings up these words loading-please wait... and within 2 seconds...
5
8938
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 data into an array of structures, with the animal and question in the same variable(question) and the bool isAnimal tells me whether its an animal or a question. If its a question it is followed by two numbers; one for the position of the line to go...
19
4416
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 practice my programming in Dev C++ rather than in VC++. What can I do now? The Game: --- writing a snake game using color console window. 1-player game. The player makes use of 4 arrow keys to operate the movement to chase and eat as much food (#)...
2
5318
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 managed to write a code for a single player (human -vs- computer) game. My problem is, now we want to merge the two games with options to pick which game you would like to play. However, the games were written totally different styles and I can't...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10452
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10169
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.