Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 15th, 2006, 1:35 AM   #1
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
C-String (error in result)

First, here's my code:

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MAXWORDS = 8000;
const int MAXWORDLENGTH = 6;
const int MINWORDLENGTH = 4;
int nWords;

void fillWords(char words[][MAXWORDLENGTH + 1], int maxwords, int& num);
int playOneRound (int wordnum);
void correct (int i);

char wordList [MAXWORDS][MAXWORDLENGTH + 1];

// fills the wordList array
void fillWords(char words[][MAXWORDLENGTH+1], int MAXWORDS, int& num) 
{
	ifstream wordfile("C:/words.txt");
	if ( ! wordfile)
	{
		cout << "Cannot open words.txt!" << endl;
		exit(1);
	}
	char line[10000];
	num = 0;
	while (wordfile.getline(line, 10000))
	{
		if (num == MAXWORDS)
		{
			cout << "Using only the first " << num
				<< " words from words.txt" << endl;
			break;
		}
		int k;
		for (k = 0; islower(line[k]); k++)
			;
		if (line[k] == '\0'  &&  k >= MINWORDLENGTH  &&  k <= MAXWORDLENGTH)
		{
			strcpy(words[num], line);
			num++;
		}
	}
}

int myRand(int myLimit)
{
	return std::rand() % myLimit;
}


// choose a secret word
// check to see if secret word is in wordList
// if in, type in a guess
// count the number of correct characters in guess
int playOneRound (int wordnum)
{                                                                           
	char guess[MAXWORDLENGTH+1];

	cout << "Probe: ";
	cin >> guess;

	for (int b=0; b < strlen(guess); b++)
	{
		if (isupper(guess[b]))
			cout << "Your probe must be a word of 4 to 6 lower case letters" << endl;
		    break;
	}

	for (int i=0; i < MAXWORDS; i++)
	{
		if (strcmp (wordList[wordnum], guess) == 0)               
		{	
			correct(i);
			break;
		}
		else                                                      
		{	
			for (int h=0; h < MAXWORDS; h++)
			{
				if ( strcmp (wordList[h], guess) == 0 )       
				{	
					int numCorrect = 0;                              
					bool boolArray [MAXWORDLENGTH] = {false};       
					for (int z=0; z < MAXWORDLENGTH; z++)            
					{
						for (int j=0; j < MAXWORDLENGTH; j++)       
						{
							
							if ((guess[z] == wordList[wordnum][j]) && (boolArray[j] == false)&& (guess[z] != '\0'))   
							{	
								boolArray [j] = true;
								break;                              
							}							
						}
					}
					for (int a=0; a < MAXWORDLENGTH; a++)
					{
						if ( boolArray [a])
						numCorrect++;
					}
					return numCorrect;
					break;
				}
			}	

			cout << "I don't know that word" << endl;       
			break;

		}
		
	}

}		


void correct (int i)
{
	cout << "You got it in " << i+1 << " probes" << endl;
	
}


int main ()
{
	fillWords (wordList, MAXWORDS, nWords);

	int rounds;
	cout << "How many rounds do you want to play? ";
	cin >> rounds;

	if (rounds < 0)
	{	
		cout << "Number of rounds must be positive" << endl;
		exit (1);
	}

	for (int a=1; a < rounds+1; a++)
	{
		cout << "Round " << a << endl;
		playOneRound (myRand(10));
		 cout << playOneRound(myRand(10))  << endl;

	}

	return 0;
}

What my assignment is set a random string in my array to be the "secret word". I then input a guess probe. If correct (secret word match guess probe), it outputs how many probes it took to get the correct answer.

If wrong, it displays how many letters were right from the secret word.
ex. secret word: abbe
guess probe: abash
correct chars: 2

I tried compiling and stepping through my code, but it gives me wrong results. For example, I type in a probe, and it prompts me to type in another probe without any output in between.

example case:

How many rounds do you want to play? 1
Round 1
Probe: aback
Probe: abet <- not supposed to appear yet!
3

What's wrong?? People said that what might be wrong is one part of my main function:

playOneRound (myRand(10));
cout << playOneRound(myRand(10))  << endl;

I might be calling playOneRound twice? But I think I need both calls?
Please help me out! I've been stuck here a long time!
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 1:45 AM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Yes you are calling playOneRound twice. The first time you are ignoring the return value and the second time you are outputting the value.
Either drop thefirst call altogether, or save the return code from the first call.
E.g. you change your code to
		 int result = playOneRound (myRand(10));
		 cout << result << endl;
The Dark is offline   Reply With Quote
Old Nov 15th, 2006, 1:55 AM   #3
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Okay, that works THANKS!

Another question is .. when I'm stepping throguh, how do I find out which random "secret word" was I using?

I have to implement the following:

How many rounds do you want to play? 3
Round 1
The secret word is 5 letters long


and to get that last line of output, I have to know the secret word.
How do I find that out?

And also, once i know it, can I put it in main and have
cout << "The secret word is" << wordList.size() << "letters long";
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 2:12 AM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
The secret word is chosen using the call to the myRand function. At the moment that is being done in the call to playOneRound.
What you need to do is to pick a random word using myRand(10) and store it in a variable, say secret. You can then find the word in the wordlist array using wordlist[secret] and you can find the length of that word using strlen. Remember to use the "secret" variable when you call playOneRound.
The Dark is offline   Reply With Quote
Old Nov 15th, 2006, 2:14 AM   #5
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Okay, I see it.

I debug, and see that wordList[wordnum] = abacus (which is my secret word)
When I probe: fish
I get numCorrect = 1

which is wrong. It should be 0.

But when I type in some other probe, it most of the time gives me the correct numCorrect.

What's going wrong?
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 2:47 AM   #6
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
A few problems I can see:
1. Your loop seems to be checking for a match on any letter in any position. If this is how it is supposed to work, then fish should get a score of 1, because of the "s"
2. Your loops for the letter checking should not go to MAXWORDLENGTH, as that is reading past the end of shorter words. The characters after the zero terminator at end of the word (e.g. fish) may be random. Your loop should check to see if the you have reached the end of the word you are looking for (e.g. check guess[z] != 0 in the "z" for loop
3. I don't know what the i loop is for - you break out of it immediately in either case in the first if test, so it doesn't look like it should be there at all.
The Dark is offline   Reply With Quote
Old Nov 15th, 2006, 3:42 PM   #7
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Hmm okay:

1. Your loop seems to be checking for a match on any letter in any position. If this is how it is supposed to work, then fish should get a score of 1, because of the "s"


2. Your loops for the letter checking should not go to MAXWORDLENGTH, as that is reading past the end of shorter words. The characters after the zero terminator at end of the word (e.g. fish) may be random. Your loop should check to see if the you have reached the end of the word you are looking for (e.g. check guess[z] != 0 in the "z" for loop


Yeah, I added to check for the the null character here:

if ((guess[z] == wordList[wordnum][j]) && (boolArray[j] == false)
&& (guess[z] != '\0'))   
{	
	boolArray [j] = true;
	break;                             
}

3. I don't know what the i loop is for - you break out of it immediately in either case in the first if test, so it doesn't look like it should be there at all.

The i loop is to check if the guess is exactly correct to the secret word. If it is, then I break out of the function. (end that round)
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 4:26 PM   #8
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
2. That is an if test inside the loop - it does not stop the loop from continuing. If you have a 7 char array (like guess) and use cin to fill it from the keyboard and the user only types four characters. The value of "guess" will be "fish\0xy" where x and y could be any character. These extra characters may match letters in the secret word.

3. You don't need to have a loop if you are always going to break out of it immediately every time, just remove the loop and the break.

4. Note that your playOneRound function doesn't always return a value (e.g. after a correct guess), so it will just be returning whatever is in memory or the return register at that point.
The Dark is offline   Reply With Quote
Old Nov 15th, 2006, 4:38 PM   #9
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
I did fix my code:

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MAXWORDS = 8000;
const int MAXWORDLENGTH = 6;
const int MINWORDLENGTH = 4;
int nWords;

void fillWords(char words[][MAXWORDLENGTH + 1], int maxwords, int& num);
int playOneRound (int wordnum);
void correct (int i);

char wordList [MAXWORDS][MAXWORDLENGTH + 1];

// fills the wordList array
void fillWords(char words[][MAXWORDLENGTH+1], int MAXWORDS, int& num) 
{
	ifstream wordfile("C:/words.txt");
	if ( ! wordfile)
	{
		cout << "Cannot open words.txt!" << endl;
		exit(1);
	}
	char line[10000];
	num = 0;
	while (wordfile.getline(line, 10000))
	{
		if (num == MAXWORDS)
		{
			cout << "Using only the first " << num
				<< " words from words.txt" << endl;
			break;
		}
		int k;
		for (k = 0; islower(line[k]); k++)
			;
		if (line[k] == '\0'  &&  k >= MINWORDLENGTH  &&  k <= MAXWORDLENGTH)
		{
			strcpy(words[num], line);
			num++;
		}
	}
}

int myRand(int myLimit)
{
	return std::rand() % myLimit;
}


// choose a secret word
// check to see if secret word is in wordList
// if in, type in a guess
// count the number of correct characters in guess
int playOneRound (int wordnum)
{                                                                           
	char guess[MAXWORDLENGTH+1];
	int i;
	int h;
	do
	{
		cout << "Probe: ";
		cin >> guess;

		for (int b=0; b < strlen(guess); b++)
		{
			if (isupper(guess[b]))
				cout << "Your probe must be a word of 4 to 6 lower case letters" << endl;
			break;
		}

		for (i=0; i < MAXWORDS; i++)
		{
			if (strcmp (wordList[wordnum], guess) == 0)  
			{	
				correct(i);
				break;
			}
			else                                                      
			{	
				for (h=0; h < MAXWORDS; h++)
				{
					if ( strcmp (wordList[h], guess) == 0 )        
					{	
						int numCorrect = 0;                              
						bool boolArray [MAXWORDLENGTH] = {false};       
						for (int z=0; z < MAXWORDLENGTH; z++)            
						{
							for (int j=0; j < MAXWORDLENGTH; j++)       
							{
								if ((guess[z] == wordList[wordnum][j]) && (boolArray[j] == false)&& (guess[z] != '\0'))   
								{	
									boolArray [j] = true;
									break;                              
								}							
							}
						}
						for (int a=0; a < MAXWORDLENGTH; a++)
						{
							if ( boolArray [a])
								numCorrect++;
						}
						cout << numCorrect << endl;
						break;
					}
				}	

				if ( strcmp (wordList[h], guess) != 0 )        
			{				
				cout << "I don't know that word" << endl;       
				break;

			}
			// 

			// wordList[wornum]

		}
	}while (strcmp (wordList[wordnum], guess) != 0);
	return i;
}		

void correct (int i)
{
	cout << "You got it in " << i+1 << " probes" << endl;
	
}


int main ()
{
	fillWords (wordList, MAXWORDS, nWords);

	int rounds;
	cout << "How many rounds do you want to play? ";
	cin >> rounds;

	if (rounds < 0)
	{	
		cout << "Number of rounds must be positive" << endl;
		exit (1);
	}

	for (int a=1; a < rounds+1; a++)
	{
		cout << "Round " << a << endl;
		
		int result = playOneRound (myRand(10));
		cout << result << endl;

	}

	return 0;
}

It does now return values (correct ones). The reason my previous didn't work was that I have a return numCorrect, and that exited the function once it happened.

I do have another question if you don't mind helping me on.



I have to enter in "The Secret word is ___ letters long" right after the Round __ and before the probe input.

Any idea where to put that?
My idea is to use strlen (wordList[wordnum]), but I have to access the secret word (wordList[wordnum]) first, possibly in my main function?

Any ideas of how to go from here?
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 6:56 PM   #10
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Re-read post #4. It explains what you need to do.

Also, it looks like your playOneRound function will always return 0.

This line at the end of the for h loop:
if ( strcmp (wordList[h], guess) != 0 )
will access outside of the wordList array if you enter a word that does not match any words in the wordlist. This may cause a problem on some systems.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 6:45 PM
A standards question, optional inputs into Methods Arla C# 4 Apr 27th, 2005 11:38 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 1:32 PM
function solomon_13000 Java 6 Apr 3rd, 2005 12:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:55 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC