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

    for (int i=0; i < MAXWORDS; i++)
    {
        if (strcmp (wordList[wordnum], guess) == 0)               // if probe is correct
        {    
            correct(i);
            break;
        }
        else                                                      // if probe not correct
        {    
            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 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++;
                    }
                    return numCorrect;
                    break;
                }
            }    

            cout << "I don't know that word" << endl;        // otherwise, output this
            break;

        }
        
    }

}        


void correct (int i)
{
    cout << "You got it in " << i+1 << " 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;
        //        cout << "The secret word is" << wordList[wordnum]   << "letters long" << endl;
        playOneRound (myRand(10));

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

    }

    return 0;
}

What it's supposed to do is take in a probe. If correct, it says how many probes it took to get the correct answer.

If wrong, it says how many letters were right from the secret word..

I tried compiling and stepping throguh, but it gives me wrong results. For example, I type in a probe, and it prompts me to type in a nother one without any output in between.

ex.
How many rounds do you want to play? 1
Round 1
Probe: aback
Probe: abet
3

Can you see what's wrong?

Bump! Please help! My code is due very soon!

I think one problem I have is that in main function, I called playOneRound one too many times .. but I hvae no idea what to fix.

Hello,

The problem is in your for-loop in main():

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

Your calling the playoneround function twice for each loop. Try to remove the line in red.

Regards Niek

Bump! Please help! My code is due very soon!

This is not a chat forum. Don't bump your posts. It's not necessary and we have lives outside the forums. Plus, your lack of planning is not an emergency for anyone but you ... sorry. We'll get to it when we get to it.

What it's supposed to do is take in a probe. If correct, it says how many probes it took to get the correct answer.

If wrong, it says how many letters were right from the secret word..

I tried compiling and stepping throguh, but it gives me wrong results. For example, I type in a probe, and it prompts me to type in a nother one without any output in between.

ex.
How many rounds do you want to play? 1
Round 1
Probe: aback
Probe: abet
3

Can you see what's wrong?

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

        [b]// you play one round and throw away the result...[/b]
        playOneRound (myRand(10));

        [b]// ... then you play another round and output the result[/b]
        cout << playOneRound(myRand(10))  << endl;
        [b]// Therefore it looks like you input twice[/b]

    }

    return 0;
}

Sorry for bumping. I was just frustrated about my lack of time left.

Anyways, just a quick question.

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

I don't know if this is valid, according to my code:

cout << "Round " << a << endl;
cout << "The secret word is" << length  << "letters long" << endl;
int result = playOneRound (myRand(10));
int length = strlen(wordList[myRand(10)]);
cout << result << endl;

The word.txt file is a bunch of 4-6 letter words.
It contains about 7000 words, but I'll only include a few to give an idea:

aback
abacus
abase
abash
abate
abater
abbas
abbe
abbey
abbot
abduct
abed
abet
abide
abject
ablate
ablaze

Anyways, I solved the previous question kind of .. but I'm stuck
on something new now .. hopefully someone can help me out.

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.

I should include the code in here, so I can point out the possible error locations ..

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

        [B]for (i=1; i < MAXWORDS; i++)   [/B]                       // i is the number of probes we're on
        {
             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;                               
                        bool boolArray [MAXWORDLENGTH] = {false};       
                         for (int z=0; z < MAXWORDLENGTH; z++)            
                         {
                            for (int j=0; j < MAXWORDLENGTH; j++)      
                             {
                                // 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; 
                    }
                 }    

                if ( strcmp (wordList[h], guess) != 0 )        // if guess is in wordlist 
                
                     cout << "I don't know that word" << endl;    // otherwise, output this
                    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 ..

Can you help me out?

I keep getting '1'

True. This is because every time you guess a word, you will jump inside the PlayOneround loop where you encouter this:

for (i=1; i < MAXWORDS; i++)              
 {
         if (strcmp (wordList[wordnum], guess) == 0)    
             {    
                correct(i);
                 break;
            }//......etc

If the entered probe was correct, this if statement will be true. Because you say i=1 (in blue) correct(i) will always output 1. So this statement is in the wrong place.
What you want to do is place it inside your main() loop, just count how many times PlayOneRound() is called. That's how many tries it took.

regards Niek

Okay, I did fix that problem.
My code looks like this now:

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

My last task (finally!) is to implement the average, minimum, and maximum # of probes in all the rounds. (Average is for each round)

I'm trying to do it in the void function correct

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

But it says rounds is undeclared. I don't know how to proceed .. should I just put this in main?

EIther you can put this code in main itself or can pass the variable "round" to the function. It says undefined since the variable "round" is out of scope of the function.

I tried this and still rounds is undeclared it says.
I don't know where to put it!!


// from playOneRound function

if (strcmp (wordList[wordnum], guess) == 0)          // if probe is correct
{    
        correct(i, rounds);
        break;
}

// from correct function

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

// from main function

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

If you callign the correct( ) function in main( ) which has the variable "round" you can do something like:

void correct( int i, int round )
{
   // do whatever you want 
}

// In main
correct( i, round ) ;

Though I dont know what you are tryign to achieve here( i havent followed the thread closely) maybe the above thing shoud be helpful to you..

The bottom line is that in whichever function you require to access the "round" variable from main( ) you need to pass round to that function.

This question has already been answered. Start a new discussion instead.