C-Strings : Bug in Code

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

C-Strings : Bug in Code

 
0
  #1
Nov 14th, 2006
  1.  
  2. #define _CRT_SECURE_NO_DEPRECATE
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstring>
  6. #include <cctype>
  7. #include <cstdlib>
  8. #include <ctime>
  9.  
  10. using namespace std;
  11.  
  12. const int MAXWORDS = 8000;
  13. const int MAXWORDLENGTH = 6;
  14. const int MINWORDLENGTH = 4;
  15. int nWords;
  16.  
  17. void fillWords(char words[][MAXWORDLENGTH + 1], int maxwords, int& num);
  18. int playOneRound (int wordnum);
  19. void correct (int i);
  20.  
  21. char wordList [MAXWORDS][MAXWORDLENGTH + 1];
  22.  
  23. // fills the wordList array
  24. void fillWords(char words[][MAXWORDLENGTH+1], int MAXWORDS, int& num)
  25. {
  26. ifstream wordfile("C:/words.txt");
  27. if ( ! wordfile)
  28. {
  29. cout << "Cannot open words.txt!" << endl;
  30. exit(1);
  31. }
  32. char line[10000];
  33. num = 0;
  34. while (wordfile.getline(line, 10000))
  35. {
  36. if (num == MAXWORDS)
  37. {
  38. cout << "Using only the first " << num
  39. << " words from words.txt" << endl;
  40. break;
  41. }
  42. int k;
  43. for (k = 0; islower(line[k]); k++)
  44. ;
  45. if (line[k] == '\0' && k >= MINWORDLENGTH && k <= MAXWORDLENGTH)
  46. {
  47. strcpy(words[num], line);
  48. num++;
  49. }
  50. }
  51. }
  52.  
  53. int myRand(int myLimit)
  54. {
  55. return std::rand() % myLimit;
  56. }
  57.  
  58.  
  59. // choose a secret word
  60. // check to see if secret word is in wordList
  61. // if in, type in a guess
  62. // count the number of correct characters in guess
  63. int playOneRound (int wordnum)
  64. {
  65. char guess[MAXWORDLENGTH+1];
  66.  
  67. cout << "Probe: ";
  68. cin >> guess;
  69.  
  70. for (int b=0; b < strlen(guess); b++)
  71. {
  72. if (isupper(guess[b]))
  73. cout << "Your probe must be a word of 4 to 6 lower case letters" << endl;
  74. }
  75.  
  76. for (int i=0; i < MAXWORDS; i++)
  77. {
  78. if (strcmp (wordList[wordnum], guess) == 0) // if probe is correct
  79. {
  80. correct(i);
  81. break;
  82. }
  83. else // if probe not correct
  84. {
  85. for (int h=0; h < MAXWORDS; h++)
  86. {
  87. if ( strcmp (wordList[h], guess) == 0 ) // if guess is in wordlist
  88. {
  89. int numCorrect = 0; // declare numCorrect
  90. bool boolArray [MAXWORDLENGTH] = {false}; // set all elements in boolArray to false
  91. for (int z=0; z < MAXWORDLENGTH; z++) // goes through each char in guess
  92. {
  93. for (int j=0; j < MAXWORDLENGTH; j++) // goes through each char in secret word
  94. {
  95. // if found match, change that element to true
  96. if ((guess[z] == wordList[wordnum][j]) && (boolArray[j] == false)&& (guess[z] != '\0'))
  97. {
  98. boolArray [j] = true;
  99. break; // break out, so we don't count matches twice
  100. }
  101. }
  102. }
  103. for (int a=0; a < MAXWORDLENGTH; a++)
  104. {
  105. if ( boolArray [a])
  106. numCorrect++;
  107. }
  108. return numCorrect;
  109. break;
  110. }
  111. }
  112.  
  113. cout << "I don't know that word" << endl; // otherwise, output this
  114. break;
  115.  
  116. }
  117.  
  118. }
  119.  
  120. }
  121.  
  122.  
  123. void correct (int i)
  124. {
  125. cout << "You got it in " << i+1 << " probes" << endl;
  126. // cout << "Average: " << (i+1)/rounds << ", minimum:" << //minimum
  127. // ", maximum:"; << // maximum << endl;
  128. }
  129.  
  130.  
  131. int main ()
  132. {
  133. fillWords (wordList, MAXWORDS, nWords);
  134.  
  135. int rounds;
  136. cout << "How many rounds do you want to play? ";
  137. cin >> rounds;
  138.  
  139. if (rounds < 0)
  140. {
  141. cout << "Number of rounds must be positive" << endl;
  142. exit (1);
  143. }
  144.  
  145. for (int a=1; a < rounds+1; a++)
  146. {
  147. cout << "Round " << a << endl;
  148. // cout << "The secret word is" << wordList[wordnum] << "letters long" << endl;
  149. playOneRound (myRand(10));
  150.  
  151. cout << playOneRound(myRand(10)) << endl;
  152.  
  153. }
  154.  
  155. return 0;
  156. }

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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Re: C-Strings : Bug in Code

 
0
  #2
Nov 15th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,936
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 305
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: C-Strings : Bug in Code

 
0
  #3
Nov 15th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: C-Strings : Bug in Code

 
0
  #4
Nov 15th, 2006
Originally Posted by aznballerlee View Post
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.


Originally Posted by aznballerlee View Post
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;

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

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

    }

    return 0;
}
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Re: C-Strings : Bug in Code

 
0
  #5
Nov 15th, 2006
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:

  1. cout << "Round " << a << endl;
  2. cout << "The secret word is" << length << "letters long" << endl;
  3. int result = playOneRound (myRand(10));
  4. int length = strlen(wordList[myRand(10)]);
  5. cout << result << endl;
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: C-Strings : Bug in Code

 
0
  #6
Nov 15th, 2006
What does the text file words.txt look like?

Provide a sample please.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Re: C-Strings : Bug in Code

 
0
  #7
Nov 15th, 2006
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:

  1. aback
  2. abacus
  3. abase
  4. abash
  5. abate
  6. abater
  7. abbas
  8. abbe
  9. abbey
  10. abbot
  11. abduct
  12. abed
  13. abet
  14. abide
  15. abject
  16. ablate
  17. 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;
         }

        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                                                  // 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,936
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 305
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: C-Strings : Bug in Code

 
0
  #8
Nov 16th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 73
Reputation: aznballerlee is an unknown quantity at this point 
Solved Threads: 0
aznballerlee aznballerlee is offline Offline
Junior Poster in Training

Re: C-Strings : Bug in Code

 
0
  #9
Nov 16th, 2006
Okay, I did fix that problem.
My code looks like this now:

  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <iostream>
  3. #include <fstream>
  4. #include <cstring>
  5. #include <cctype>
  6. #include <cstdlib>
  7. #include <ctime>
  8.  
  9. using namespace std;
  10.  
  11. const int MAXWORDS = 8000;
  12. const int MAXWORDLENGTH = 6;
  13. const int MINWORDLENGTH = 4;
  14. int nWords;
  15.  
  16. void fillWords(char words[][MAXWORDLENGTH + 1], int maxwords, int& num);
  17. int playOneRound (int wordnum);
  18. void correct (int i);
  19.  
  20. char wordList [MAXWORDS][MAXWORDLENGTH + 1];
  21.  
  22. // fills the wordList array
  23. void fillWords(char words[][MAXWORDLENGTH+1], int MAXWORDS, int& num)
  24. {
  25. ifstream wordfile("C:/words.txt");
  26. if ( ! wordfile)
  27. {
  28. cout << "Cannot open words.txt!" << endl;
  29. exit(1);
  30. }
  31. char line[10000];
  32. num = 0;
  33. while (wordfile.getline(line, 10000))
  34. {
  35. if (num == MAXWORDS)
  36. {
  37. cout << "Using only the first " << num
  38. << " words from words.txt" << endl;
  39. break;
  40. }
  41. int k;
  42. for (k = 0; islower(line[k]); k++)
  43. ;
  44. if (line[k] == '\0' && k >= MINWORDLENGTH && k <= MAXWORDLENGTH)
  45. {
  46. strcpy(words[num], line);
  47. num++;
  48. }
  49. }
  50. }
  51.  
  52. int myRand(int myLimit)
  53. {
  54. return std::rand() % myLimit;
  55. }
  56.  
  57. bool hasUpperCaseLetters(char guess[MAXWORDLENGTH+1])
  58. {
  59. for (int b=0; b < strlen(guess); b++)
  60. {
  61. if (isupper(guess[b]))
  62. return true;
  63. }
  64. return false;
  65. }
  66.  
  67. bool doesNotExist (char guess[MAXWORDLENGTH+1])
  68. {
  69. for (int h=0; h < MAXWORDS; h++)
  70. {
  71. if (strcmp (wordList[h], guess) == 0) // check if guess is not in wordList
  72. return false;
  73. }
  74. return true;
  75. }
  76.  
  77. // choose a secret word
  78. // check to see if secret word is in wordList
  79. // if in, type in a guess
  80. // count the number of correct characters in guess
  81. int playOneRound (int wordnum)
  82. {
  83. char guess[MAXWORDLENGTH+1];
  84. int i = 0;
  85. int h;
  86.  
  87. do
  88. {
  89. cout << "Probe: ";
  90. cin >> guess;
  91. i++;
  92.  
  93. for (int b=0; b < strlen(guess); b++)
  94. {
  95. if (hasUpperCaseLetters (guess))
  96. {
  97. cout << "Your probe must be a word of 4 to 6 lower case letters" << endl;
  98. break;
  99. }
  100.  
  101.  
  102. if (doesNotExist (guess))
  103. {
  104. cout << "I don't know that word" << endl;
  105. break;
  106. }
  107.  
  108. }
  109.  
  110. if (strcmp (wordList[wordnum], guess) == 0) // if probe is correct
  111. {
  112. correct(i);
  113. break;
  114. }
  115. else // if probe not correct
  116. {
  117. for (h=0; h < MAXWORDS; h++)
  118. {
  119. if ( strcmp (wordList[h], guess) == 0 ) // if guess is in wordlist
  120. {
  121. int numCorrect = 0; // declare numCorrect
  122. bool boolArray [MAXWORDLENGTH] = {false}; // set all elements in boolArray to false
  123. for (int z=0; z < MAXWORDLENGTH; z++) // goes through each char in guess
  124. {
  125. for (int j=0; j < MAXWORDLENGTH; j++) // goes through each char in secret word
  126. {
  127. // if found match, change that element to true
  128. if ((guess[z] == wordList[wordnum][j]) && (boolArray[j] == false)&& (guess[z] != '\0'))
  129. {
  130. boolArray [j] = true;
  131. break; // break out, so we don't count matches twice
  132. }
  133. }
  134. }
  135. for (int a=0; a < MAXWORDLENGTH; a++)
  136. {
  137. if ( boolArray [a])
  138. numCorrect++;
  139. }
  140. cout << numCorrect << endl;
  141. break;
  142. }
  143. }
  144.  
  145. }
  146.  
  147. }
  148. while (strcmp (wordList[wordnum], guess) != 0);
  149. return i;
  150. }
  151.  
  152. void correct (int i)
  153. {
  154. cout << "You got it in " << i << " probes" << endl;
  155. // cout << "Average: " << (i+1)/rounds;
  156. // << ",minimum:" << //minimum
  157. // ", maximum:"; << // maximum << endl;
  158. }
  159.  
  160.  
  161. int main ()
  162. {
  163. fillWords (wordList, MAXWORDS, nWords);
  164.  
  165. int rounds;
  166. cout << "How many rounds do you want to play? ";
  167. cin >> rounds;
  168.  
  169. if (rounds < 0)
  170. {
  171. cout << "Number of rounds must be positive" << endl;
  172. exit (1);
  173. }
  174.  
  175. for (int a=1; a < rounds+1; a++)
  176. {
  177. cout << "Round " << a << endl;
  178. int random = 5; //myRand(10);
  179. int length = strlen(wordList[random]);
  180. cout << "The secret word is " << length << " letters long" << endl;
  181. int result = playOneRound (random);
  182.  
  183. }
  184.  
  185. return 0;
  186. }
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

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

But it says rounds is undeclared. I don't know how to proceed .. should I just put this in main?
Last edited by aznballerlee; Nov 16th, 2006 at 3:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: C-Strings : Bug in Code

 
0
  #10
Nov 16th, 2006
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 don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC