#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; }
for (int a=1; a < rounds+1; a++)
{
cout << "Round " << a << endl;
playOneRound (myRand(10));
cout << playOneRound(myRand(10)) << endl;
}Bump! Please help! My code is due very soon!
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;
}
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;
aback abacus abase abash abate abater abbas abbe abbey abbot abduct abed abet abide abject ablate ablaze
#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;
}I keep getting '1'
for (i=1; i < MAXWORDS; i++)
{
if (strcmp (wordList[wordnum], guess) == 0)
{
correct(i);
break;
}//......etc
#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; }
void correct (int i) { cout << "You got it in " << i << " probes" << endl; cout << "Average: " << (i+1)/rounds; }
| DaniWeb Message | |
| Cancel Changes | |