There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen boot bsod computer connection crash css dell display driver drivers email error ethernet excel explorer firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook express partition password printer problem router slow software sound trojan usb video virus vista windows windows xp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Solved: question: do while loop


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
Ghaleon's Avatar
Senior Member with 282 posts.
 
Join Date: Dec 2004
Experience: Beginner
08-Dec-2005, 03:24 AM #1
Solved: question: do while loop
me again,

i'm writing the game hang man. I have the bulk of it working, but i'm supposed to have it repeat as often as the user prompts it to; this is where i'm stuck.

no errors this time, but when it is told to repeat, it selects a new word and then immediately goes back to the old one. i'm sorry if i'm being too vague... here's my code:

Code:
#include <iostream.h>
#include "wordBank.h"
#include <string.h>
#include "Gallows.h"
int drawWord (char x [21], char guess, int length);

void main ()

{
/*	char x [21], guess, guessed[21];
	int hit,Misses=0,i=0, end=0, length=0, repeat=0, win=0;
	static int turn=0;
	Gallows g;
	wordBank y;
	*/
	int repeat=0;
	do
	{
	
		char x [21], guess, guessed[21];
		int hit,Misses=0,i=0, end=0, length=-1, win=0;
		static int turn=0;
		Gallows g;
		wordBank y;

		
		y.readfile();
		strcpy(x,y.getRandomWord());
		cout<<x;

		g.draw(Misses);

		y.setWordLength(x);
		length=y.getWordLength();
		cout<<endl<<length;

		while (end==0)
		{
			
		
			cout<<endl<<"guess a letter\n";
			win=drawWord(x, guess, length);
			cout<<endl;
			cin>>guess;
		
			turn++;

			hit=y.findLetter(guess, x);
			g.setMisses(hit, x, guess);
			Misses=g.getMisses();
			g.draw(Misses);

			cout<<"you have guessed: ";
			int n=g.getNumGuessed();
			g.setGuessed(guess);
			int num=g.getNumGuessed();
			for(i=0; i<num; i++)
			{
				cout<<g.getGuessed(i);
				guessed[i]=g.getGuessed(i);


			}
	
			
			if (Misses==6)
			{
				cout<<"you loose\n";
				end=1;
			}
		
			if (win==length || Misses==6)
			{

				cout<<endl<<"would you like to play again(0=no, 1=yes)?\n";
				cin>>repeat;
				if(repeat==1)
				{
					turn=0;
					Misses=0;
					end=1;
					length=0;
					for (int i=0; guessed[i]!='\0'; i++)
					{
						guessed[i]=' ';
					}
				}
			}

			
		}

	}while(repeat==1);
}

int drawWord(char x[21], char guess, int length)
{
	static char display[21]="";
	int win=0;
	
	for (int i=0; x[i]!='\0'; i++)
	{
		if (guess==x[i])
		{
			display[i]=guess;
		}

		else if (guess!=x[i] && display[i]!='A'&& display[i]!='B'&& display[i]!='C'&& display[i]!='D'
		           	    && display[i]!='E'&& display[i]!='F'&& display[i]!='G'&& display[i]!='H'
			            && display[i]!='I'&& display[i]!='J'&& display[i]!='K'&& display[i]!='L'
				        && display[i]!='M'&& display[i]!='N'&& display[i]!='O'&& display[i]!='P'
				        && display[i]!='Q'&& display[i]!='R'&& display[i]!='S'&& display[i]!='T'
					    && display[i]!='U'&& display[i]!='V'&& display[i]!='W'&& display[i]!='X'
					    && display[i]!='Y'  && display[i]!='Z'  && display[i]!='a'&& display[i]!='b'
						&& display[i]!='c'&& display[i]!='d'&& display[i]!='e'&& display[i]!='f'
						&& display[i]!='g'&& display[i]!='h'&& display[i]!='i'&& display[i]!='j'
						&& display[i]!='k'&& display[i]!='l'&& display[i]!='m'&& display[i]!='n'
						&& display[i]!='o'&& display[i]!='p'&& display[i]!='q'&& display[i]!='r'
						&& display[i]!='s'&& display[i]!='t'&& display[i]!='u'&& display[i]!='v'
						&& display[i]!='w'&& display[i]!='x'&& display[i]!='y'&& display[i]!='z')
		

				
		{
			display[i]='-';
		}

		if(display[i]!='-')
		{
			win++;
		}

	}

	cout<<endl<<display;
	return win;
}
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 6,560 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
08-Dec-2005, 07:17 AM #2
Ghaleon, I do not know your code, I assume it is VB, but I do know logic in programming.
Ask yourself this, if the do loop is the problem, where do you set the old Word = new Word.
How can it then skip back to the old word, ie do you need to also reset something else.
If you explain where the word is reset I might be able to help.
More likely when you try and explain it to me (with nil understanding) it will strike you what is wrong. At least that always worked for me with BASIC.
__________________
.
.
OBP
I do not give up easily
winterfrost's Avatar
Senior Member with 120 posts.
 
Join Date: Nov 2005
Location: Canada
08-Dec-2005, 01:10 PM #3
I'm really just learning C, but I am a programmer, so if you don't mind a few more shot-in-the-dark suggestions...

Quote:
y.setWordLength(x);
length=y.getWordLength();
It's a little hard to understand without seeing what setWordLength and getWordLength are doing. From what I can see "y" is an object containing a list of words. You're pulling a random word out of this list (getRandomWord) and copying it to x. Then you're setting a word length on the list of words(?) to the length of x, then getting that length back from the wordlist...?

Assuming that "cout<<x;" is actually outputting the correct new word, my guess is that the problem is in this line "y.setWordLength(x);"

If "cout<<x;" isn't outputting the new word, then the problem obviously has to be in your "getRandomWord()" function.
__________________

Alter-Ego profile migration software
Hard-to-find tech tips and solutions
Beta testers needed for SwitchRight, NTFS permission replacement utility
Ghaleon's Avatar
Senior Member with 282 posts.
 
Join Date: Dec 2004
Experience: Beginner
08-Dec-2005, 02:43 PM #4
OBP, you were right. trying to explain it turned up the problem.

I was getting the new word, and the new word length but I was not re setting the display string in the drawWord function when I lost. to correct this I had to define static int display[21] in main, and then add the following code
Code:
if (Misses==6)
				{
					cout<<endl<<"you loose\n";
					cout<<"the word was: "<<x<<endl;
					
					//this loop runs back through the display array and resets all characters
					//to blanks
					for (int i=0; display[i]!='\0'; i++)
					{
						display[i]=' ';
					}
			
				}
		
				if (win==length)
				{
					cout<<endl<<"you win\n";	
			
					//this loop runs back through the display array and resets all characters
					//to blanks
					for (i=0; display[i]!='\0'; i++)
					{
						display[i]=' ';
					}
does this make sense? Thanks for the help, everyone.
OBP's Avatar
OBP OBP is offline
Computer Specs
Distinguished Member with 6,560 posts.
 
Join Date: Mar 2005
Location: UK
Experience: An old Basic Programmer
09-Dec-2005, 06:35 AM #5
Ghaleon, I am glad to have been of (limited) help, I didn't even realize it was C you were programming in but all programming is basically the same set of problems isn't it.
Can you mark this thread as "solved" using the tread tools at the top of the page?
__________________
.
.
OBP
I do not give up easily
Ghaleon's Avatar
Senior Member with 282 posts.
 
Join Date: Dec 2004
Experience: Beginner
09-Dec-2005, 10:30 AM #6
will do. thanks again.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:20 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.