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, 7:18 PM   #11
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
I did fix that error after reading the post. Thanks!
But I do have another problem:

I get the correct probe (probe match secret word) but I don't get the correct (You got it in ___ probes). I keep getting '1', which means my for loop that causes that output doesn't update properly.

Here is my current 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=1; i < MAXWORDS; i++)                          // i is the number of probes we're on
        {
             if (strcmp (wordList[wordnum], guess) == 0)         // if probe is correct
             {    
                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; 


             }

         }

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

void correct (int i)
 {
    cout << "You got it in " << i << " 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 random = myRand(10);
        int length = strlen(wordList[random]);
         cout << "The secret word is " << length << " letters long" << endl;
         int result = playOneRound (random);

     }

    return 0; 
}

Pretty much the i in the for loop doesn't iterate .. so I keep getting stuck with the same 'i' when I run the void function .. and therefore it keeps giving me '1 probes' no matter what.

There's probably a problem inside the for loop that causes this .. but I can't seem to fine it .. I hope this code isn't too hard to read.
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 7:26 PM   #12
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
That is because your function now always returns i and uses i as the parameter for the call to correct(). As far as I can tell, i has nothing to do with the number of probes you have entered and has nothing to do with anything. In fact it is now always 1 as the loop always hits a break statement in the first iteration.

You should add a counter variable to your function and increment the counter every time the user enters a probe. Then just use the counter in the call to correct() and in the return.
The Dark is offline   Reply With Quote
Old Nov 15th, 2006, 7:31 PM   #13
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Okay, I sort of get that idea.
So to solve this, I would:

set antoher variable (int p=0) at the begining of that function
and everytime I increment when a new probe is entered.

then, I delete the return i, and replace it with return p?
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 7:35 PM   #14
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Yep pretty much, although I wouldn't call it p, i would call it numGuesses. You also want to pass it in the call to the "correct" function.
The Dark is offline   Reply With Quote
Old Nov 15th, 2006, 9:53 PM   #15
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Alright gotcha Dark. I implemented something of the same idea, and that part is taken care of.

Just another part I have to worry about is when I have probe: GGGg
or something of uppercase.

It outputs two stuff: "Your probe must be a word of 4 to 6 lower case letters" and "I don't know that word", when it only is supposed to do so once.

So I'm trying to call a function so that makes it only say "Your probe must be a word of 4 to 6 lower case letters" when the probe is in upper case:

Can you tell me if this is right? I plan to call this function inside the for loop: for (b=0; b < strlen(guess); b++)

int case (guess[b], wordList[h])
{

    if (isupper(guess[b]))          // if there are any uppercase letters
    {
        cout << "Your probe must be a word of 4-6 ... " << endl;
        break;
    }

    if ( strcmp (wordList[h], guess) != 0 )        // if guess is not in wordlist
    {
        cout << "I don't know that word" << endl;
    }
}
aznballerlee is offline   Reply With Quote
Old Nov 15th, 2006, 10:32 PM   #16
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Actually I feel that's the above is totally wrong.
Here's my newer code:

bool (hasUpperCaseLetters(guess))
{
   if (isupper(guess[b]))
      return true;
}

bool (doesNotExist (guess))
{
   if (strcmp (wordList[h], guess) != 0)
       return true;
}

for (int b=0; b < strlen(guess); b++)
{
if (hasUpperCaseLetters (guess))
        cout << "Your probe must be 4-6 letters long" << endl;
        break;

if (doesNotExist (guess))
       cout << " I don't know that word" << endl;
       break;

// stuff i had in my function 

}
this is my new method .. I hope this can work out.
Intuitively, do you think this would work??
aznballerlee is offline   Reply With Quote
Old Nov 16th, 2006, 2:04 AM   #17
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
It should work, but you will need to make it proper c++ - the function names should not have brackets around them. and you need to give your parameters a type.

hasUpperCaseLetters needs to loop through all the letters in guess - i.e. until guess[b] is '\0'.

doesNotExist needs to loop through all the words and compare them with guess - i.e. h goes from 0 to nWords - 1

Then you should remove the loop around the function calls.
The Dark is offline   Reply With Quote
Old Nov 16th, 2006, 2:11 AM   #18
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Yes, I fixed those errors. Thanks for pointing them out:

I have one last bug to fix now:
bool doesNotExist (char guess[MAXWORDLENGTH+1])
{
   for (int h=0; h < MAXWORDS; h++)
   {
	if (strcmp (wordList[h], guess) != 0)     // check if guess is not in wordList
       return true;
   }
   return false;
}


this is in another function:
if (doesNotExist (guess))
{
      cout << " I don't know that word" << endl;
	break;
}

What my problem is that in the bool doesNotExist function, when I run the loop, I get the return true prematurely. For example, if the first element I'm comparing is not in wordList, I return true already. When what if the second element that I'm comparing IS in wordList, I can't reverse this.

So somehow I have to change the code .. maybe include a break .
My point in that function is to return true when I go throguh each element in wordList and find that guess is not in there. If guess is in wordList, then I return false.
aznballerlee is offline   Reply With Quote
Old Nov 16th, 2006, 2:17 AM   #19
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
Your function is returning as soon as it finds a word that does not match guess.
What it needs to do is return as soon as it finds a word that matches guess and return false. Return true if you drop out of the loop by reaching the end of the list.
So change your != 0 to == 0 and your true to false and your false to true.

Also, you should be looping up to nWords, not MAXWORDS as there is no reason to rely on your input file containing exactly 8000 words.
The Dark is offline   Reply With Quote
Old Nov 16th, 2006, 2:51 PM   #20
aznballerlee
Hobbyist Programmer
 
Join Date: Nov 2006
Posts: 111
Rep Power: 3 aznballerlee is on a distinguished road
Yes! Thanks Dark for the explanation. It took me hours to think about that, and your code worked out.

My last task (yes!) is to compute the average, minimum, and maximum amount of probes in each round.

I was thinking about implementing those in my void correct function:

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

I'm starting with average, and that causes a problem because rounds is not declarede it says. How should I go upon on this .. ?

Here's my updated 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;
}

bool hasUpperCaseLetters(char guess[MAXWORDLENGTH+1])
{
	for (int b=0; b < strlen(guess); b++)
	{
		if (isupper(guess[b]))
			return true;
	}
	return false;
}

bool doesNotExist (char guess[MAXWORDLENGTH+1])
{
	for (int h=0; h < MAXWORDS; h++)
	{
		if (strcmp (wordList[h], guess) == 0)     // check if guess is not in wordList
			return false;
	}
	return true;
}

// 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 = 0;
	int h;

	do
	{
		cout << "Probe: ";
		cin >> guess;
		i++;

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


			if (doesNotExist (guess))
			{
				cout << "I don't know that word" << endl;
				break;
			}

		}

		if (strcmp (wordList[wordnum], guess) == 0)               // if probe is correct
		{	
			correct(i);
			break;
		}
		else                                                      // if probe not correct
		{	
			for (h=0; h < MAXWORDS; h++)
			{
				if ( strcmp (wordList[h], guess) == 0 )        // if guess is in wordlist
				{	
					int numCorrect = 0;                              // declare numCorrect
					bool boolArray [MAXWORDLENGTH] = {false};       // set all elements in boolArray to false
					for (int z=0; z < MAXWORDLENGTH; z++)            // goes through each char in guess
					{
						for (int j=0; j < MAXWORDLENGTH; j++)       // goes through each char in secret word
						{
							// if found match, change that element to true
							if ((guess[z] == wordList[wordnum][j]) && (boolArray[j] == false)&& (guess[z] != '\0'))   
							{	
								boolArray [j] = true;
								break;                              // break out, so we don't count matches twice
							}							
						}
					}
					for (int a=0; a < MAXWORDLENGTH; a++)
					{
						if ( boolArray [a])
							numCorrect++;
					}
					cout << numCorrect << endl;
					break;
				}
			}	

		}

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

void correct (int i)
{
	cout << "You got it in " << i << " probes" << endl;
	//	cout << "Average: " << (i+1)/rounds;
	//	<< ",minimum:" << //minimum 
	//	 ", maximum:"; << // maximum << 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 random = 5; //myRand(10);
		int length = strlen(wordList[random]);
		cout << "The secret word is " << length << " letters long" << endl;
		int result = playOneRound (random);

	}

	return 0;
}
aznballerlee 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:58 AM.

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