![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2006
Posts: 111
Rep Power: 3
![]() |
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! |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |