| | |
C-Strings : Bug in Code
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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?
Hello,
The problem is in your for-loop in main():
Your calling the playoneround function twice for each loop. Try to remove the line in red.
Regards Niek
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;
}Regards Niek
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;
// 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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 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:
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:
C++ Syntax (Toggle Plain Text)
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;
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
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:
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 ..
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?
It contains about 7000 words, but I'll only include a few to give an idea:
C++ Syntax (Toggle Plain Text)
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;
}
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?
•
•
•
•
I keep getting '1'
for (i=1; i < MAXWORDS; i++)
{
if (strcmp (wordList[wordnum], guess) == 0)
{
correct(i);
break;
}//......etcWhat 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
•
•
Join Date: Oct 2006
Posts: 73
Reputation:
Solved Threads: 0
Okay, I did fix that problem.
My code looks like this now:
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
But it says rounds is undeclared. I don't know how to proceed .. should I just put this in main?
My code looks like this now:
C++ Syntax (Toggle Plain Text)
#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; }
I'm trying to do it in the void function correct
C++ Syntax (Toggle Plain Text)
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?
Last edited by aznballerlee; Nov 16th, 2006 at 3:44 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: adjacency lists
- Next Thread: Trying to creating an array from a text file
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






