I'm having a hard time generating a "secret word" that is random. I'm taking a random word from the wordList array, and using it in the playOneRound function.

Here is part of my instructions:

Using wordList[wordnum] as the secret word, this function 
plays one round of the game. It returns the score for that round. 
In the transcript above, this function is responsible for this much 
of the round 1 output, no more, no less:

    Probe: assert
    3
    Probe: xyzzy
    I don't know that word
    Probe: bred
    2
    Probe: mucus
    0
    Probe: never
    4
    Probe: raven

Your program must call this function to play each round of the game.

and here is what I have so far: (it is very wrong)

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

using namespace std;

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

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

char wordList [MAXWORDS][MAXWORDLENGTH + 1];



void fillWords(char words[][MAXWORDLENGTH+1], int maxwords, int& num)
{
    ifstream wordfile("Z:/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++;
        }
    }
}

 // choose a secret word
// check  if secret word is in wordList
// if in, type in a guess
// count # of correct characters in guess


int playOneRound (int wordnum)
{                                 
     for (int k = 0; k < wordnum; k++)    
     {                            
            int random = rand();
        char answer[MAXWORDS];
        char wordList [random] = answer;
        
        cout << "Probe:";
        cin >> // guess;
            if (strcpy (answer , guess ) == 0)
                break;
            else
             // count number of correct characters

    }

}


int main ()
{
    int rounds;
    cout << "How many rounds do you want to play?" << endl;
    cin >> rounds >> endl;

    for (int i=1, i < rounds +1; i++)
    {
        cout << "Round" << i << endl;

    
    
    
    }

You're making progress, but there's definitely some errors/problems.

You might find string to be easier and nicer to use than char -- it's old, and although it's not "outdated" technically, most people prefer string.

I have NO idea what you're trying to do here:

int playOneRound (int wordnum)
{                                 
     for (int k = 0; k < wordnum; k++)    
     {                            
            int random = rand();
        char answer[MAXWORDS];
        char wordList [random] = answer;

Why are you declaring a new answer[] array every time you iterate? And secondly, you're assigning a wordList element some random junk contained in the array you just created. You'll need to fix that.

In playOneRound you still need to declare a guess variable -- I can see you've already started using it, but you'll definintely need a string array there.

I obviously can't go through each line of code you posted and tell you what you need to fix, change and add. It's up to you to figure out exactly how you should code it. My suggestion:

Start simple. Make it more complex as you need it. For example, you have fillWords() to open a file and load words into memory. To make it simpler at first, you may want to simply have a global array:

string wordList[3] = { {"Word1"}, {"Word2"}, {"Word3"} };

Code the essential parts first. Is loading the file really the essential part in this project? Even though it might be required, that's not the goal, so you can ignore it until you've got the core working. It's easier to debug when you're only working at 1 bug at a time, and it's also more motivating when you know at least part of your program works.

Okay, thanks.

Can you tell me what exactly this codes does? And how it's useful in my task?

void fillWords(char words[][MAXWORDLENGTH+1], int maxwords, int& num)
{
    ifstream wordfile("Z:/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 playOneRound (int wordnum)
{
  for (int k = 0; k < wordnum; k++)
  {
     int random = rand();
     char answer[MAXWORDS];
     char wordList [random] = answer;
   }
}

This part was meant to ..

1) loop is for each round of game
2) choose random integer
3) set up wordList string array
4) set a new "answer", with random integer
corresponsindg to the position in array word list.

I understand your advice to start simple, but I have no idea why this would help me? (since didn't the fillWords function already fill the array for me?

string wordList[3] = { {"Word1"}, {"Word2"}, {"Word3"} };

Sorry about my syntax error -- the string array was supposed to be like this:

string wordList[3] = {"Word1", "Word2", "Word3"};

Can you tell me what exactly this codes does? And how it's useful in my task?

I understand your advice to start simple, but I have no idea why this would help me? (since didn't the fillWords function already fill the array for me?

That code simply loads words from a text file. The reason I said to use an array for now is that if there *are* any bugs in there (I can't see any, but doesn't mean there aren't any), you only have to worry about the other ones at the moment. They key is to get the core working, then work on special features.

This part was meant to ..

1) loop is for each round of game
2) choose random integer
3) set up wordList string array
4) set a new "answer", with random integer
corresponsindg to the position in array word list.

Hmm... from what I understand, why don't you just go like this:

answer = wordList[random];

Of course, you'll need to use strcpy if you're still planning on using char for your charectar arrays.

Hope this helps

Alright, I will try to take those in consideration.
Meanwhile, I updated my code. Can you take a look
to see if what I have looks correct?

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

using namespace std;

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

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

char wordList [MAXWORDS][MAXWORDLENGTH + 1];

// fills the wordList array
void fillWords(char words[][MAXWORDLENGTH+1], int MAXWORDS, int& num) 
{
    ifstream wordfile("Z:/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++;
        }
    }
}

// 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)
{                                                                           
    wordnum = rand();
    string guess[MAXWORDS];
    string wordList[wordnum];
        
    cout << "Probe: ";
    cin >> guess[MAXWORDS];
    
    for (int i=0; i<MAXWORDS; i++)
    {
        if (strcpy (wordList[wordnum], guess[MAXWORDS]) == 0)
            break;
        else
            if ( // if guess is not in wordList) 
               )
                cout << "I don't know that word" << endl;
            else    
                // count number of correct characters
    }
}


int main ()
{
    fillWords (wordList, MAXWORDS, nWords);
    
    int rounds;
    cout << "How many rounds do you want to play?" << endl;
    cin >> rounds >> endl;

    for (int i=1, i < rounds +1; i++)
    {
        cout << "Round" << i << endl;
        playOneRound (rand());

    
    }
    return 0;
}

I altered my code a bit, but I still get some compilation errors. It would be great if someone could tell me what's going wrong here so I could proceed from here. The entire code (what i have so far) is in my previous post.

if (strcmp (wordList[wordnum][MAXWORDLENGTH + 1], guess[MAXWORDS]) == 0)

error C2664: 'strcmp' : cannot convert parameter 1 from 'char' to 'const char *'

char wordList[wordnum][MAXWORDLENGTH + 1];    // secret word

error C2466: cannot allocate an array of constant size 0
error C2133: 'wordList' : unknown size

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

    }

error C2086: 'int a' : redefinition

You should put your globals before the function prototypes -- otherwise you might get some errors.

Also, you shouldn't mix charectar array types: in a number of places you're using char , although you've used some strings in playOneRound . Mixing types makes it more difficult when performing operatings on it, for example here:

if (strcpy (wordList[wordnum], guess[MAXWORDS]) == 0)

What's happening here is wordList is a char array, whereas guess is a string. This of course will not work, because strcpy() is only meant to copy chars. Hope this makes sense.

It's coming along nicely though, and I'm sure you won't have too many problems completing this.
[edit]Oops, as I was typing up my post, you added a new one!
Anyway, the first error will be solved by putting the globals first (it's the constants that are causing problems). strcmp() is having problems because you're mixing types as I said previously, and the error about wordList is that you can't use a variable (wordnum) as an array size. [/edit]

1. I don't think of another way to compare to see if the
random word selected from the wordList is equal to my guess ..
I understand that the following doesnt work, because I'm comparing char with string ..

if (strcmp (wordList[wordnum][MAXWORDLENGTH + 1], guess[MAXWORDS]) == 0)

2. I put all my globals ahead of the prototypes already:

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);

but I still get that error.

3. My specs say that I should use wordList [wordnum] as the secret word ..

should I just use

char wordList[MAXWORDS][MAXWORDLENGTH + 1];

as my secret word instead??

4. I also get this error that I have to redefine a

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

    
    }

I see no problem though ..

Ahh never mind the previous.
I fixed it up with these changes:

char guess[MAXWORDLENGTH+1];
char wordList[MAXWORDS][MAXWORDLENGTH+1];


if (strcmp (wordList[wordnum], guess) == 0)

I think I'll need help in the next part .. or soon .. when I have to tally up the number of correct characters. I'll consult this when I approach there.

1. I don't think of another way to compare to see if the
random word selected from the wordList is equal to my guess ..
I understand that the following doesnt work, because I'm comparing char with string ..

if (strcmp (wordList[wordnum][MAXWORDLENGTH + 1], guess[MAXWORDS]) == 0)

No. What you should do is use all strings -- or all chars. In this case, I think you should use string , because then the comparison would be as simple as:

if (wordList[wordnum] == guess[MAXWORDS])

[deleted the rest because it's not needed]

I progressed a bit more, and this compiles, but there's still some bugs that I c an't seem to fix.

#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);

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)
{                                                                           
        wordnum = myRand(10);
        char guess[MAXWORDLENGTH+1];
                    
        cout << "Probe: ";
        cin >> guess[MAXWORDLENGTH+1];
        
        for (int i=0; i<MAXWORDS; i++)
        {
            if (strcmp (wordList[wordnum], guess) == 0)
                break;
            else
            {    
                for (int 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 (guess[z] == wordList[wordnum][j])        // if found match, change that element to true
                                {    
                                    boolArray [j] = true;
                                     break;                              // break out, so we don't count matches twice
                                }                            
                            }
                        }
                            for (int a=0; a<MAXWORDLENGTH; a++)
                            {
                                if ( boolArray [a])
                                    numCorrect++;
                            }
                            return numCorrect;
                    }
                    else
                        cout << "I don't know that word" << endl; 
                        break;
                                                                          // otherwise, output this
                
                }
                        
            }
                
        }        
}

int main ()
{
    fillWords (wordList, MAXWORDS, nWords);
    
    int rounds;
    cout << "How many rounds do you want to play? ";
    cin >> rounds;
    
     for (int a=1; a < rounds +1; a++)
    {
        cout << "Round" << a << endl;
        playOneRound (myRand (10));
    
    }


    /* std::srand(std::time(0));
    for (int k = 0; k < 1000; k++)
        cout << " " << myRand(10);
*/ 
    return 0;
}

Here's my code so far, and when I try to test different probes, I get the wrong results. For example, when I access a word from the list (aback), it returns me "I don't know this word" infinite amount of times.

What's going on?

This article has been dead for over six months. Start a new discussion instead.