-
problem with variables
is picking a rand() value wich is used for rand element index from a string array, that holds many words. 22 is just the maxoffset of rand().
is returning the picked word
So this first code is getting the lenth of a string and randWord without a problem. Also it prints its elements in the right way.
Code:
Set_randWord(22);
int len = Get_randWord().size();
cout << len << endl; //<-- lenth of word is OK
cout << Get_randWord() << endl; //<-- return a word is OK
for (int i = 0; i < len; i++){
hiddenWord[i] = '_'; //<-- hiddenWord declared in the class .h
cout << hiddenWord[i] << " ";
}
hiddenWord[len] = '\0';
So the second code, the only difference is that the lenth variable of word is previously declared in the class.h
Code:
Set_randWord(22);
lenofWord = Get_randWord().size(); //<-- lenofWord declared in .h
cout << lenofWord << endl; //<-- lenth of word is OK
cout << Get_randWord() << endl; //<-- return a word is OK
for (int i = 0; i < lenofWord; i++){
hiddenWord[i] = '_';
cout << hiddenWord[i] << " ";
}
hiddenWord[lenofWord] = '\0';
In the second code i get exactly 195 '_' characters no matter how ,uch the lenth of the word is or the random word itself, 195 '_' are printed.
The difference i noticed is only that i change with previously declared Ty for reading!
-
Are these two independent segments of code or are they used inside the same program?
-
Inside the same program, but i make one of the segments as a comment so only one to be run'd.
-
Code:
lenofWord = Get_randWord().size(); //<-- lenofWord declared in .h
cout << lenofWord << endl; //<-- lenth of word is OK
cout << Get_randWord() << endl; //<-- return a word is OK
Tell us what you actually see when you print these debug lines.
Does Get_randWord() return the same answer each time you call it?
How does lenofWord compare with the actual declaration of hiddenWord?
Did you count the \0 when setting the array size?
-
Code:
hiddenWord[lenofWord] = '\0';
here i set the
end of the array size. I do not declare any previous size.
In the .h i declare the hiddenWord as When i call the ctor of the class, there is a function that store all the lines from a .txt file (each word is in new line) in array of string, each element holds a word.
Set_RandWord() picks element index and Get_randWord return that string element index wich is the word.
is just a char array where will be filled with '_' characters and later replaced with letters.
is just an integer that stores the lenth of the chosen word.
Im runing this function in a clear main() without any code.
-
You've left out all sorts of things that are required to see what the problem is.
Please post a minimal complete example which we can compile that reproduces the problem.
-
> In the .h i declare the hiddenWord as
> char hiddenWord[];
So basically, you have NO space allocated for your array at all.
Then you merrily trash someone else's memory with the for loop.
Post your WHOLE code, and I'm sure we'll find some more errors as well.
-
MYCLASS.cpp download - 2shared .cpp
MYCLASS.h download - 2shared .h
words1.txt download - 2shared .txt
in the main only call the ctor and
Code:
CreateHiddenWord();
PS: Sorry for the downloadable files, the class usnt big at all, its readable please take the time
PS: This is a second project only for testing class and functions etc.
-
The code (don't force people to download your code):
Code:
#include "MYCLASS.h"
MYCLASS::MYCLASS(){ //ctor
_wordBuf = new string[CountLinesOfFile ("words1.txt")];
WordsBuffer (_wordBuf, "words1.txt");
}
MYCLASS::~MYCLASS(){ //dtor
//dtor
}
MYCLASS::MYCLASS(const MYCLASS& other){ //copy ctor
//copy ctor
}
void MYCLASS::WordsBuffer (string* _arrName, const char* fname){
string line;
int index = 0;
ifstream file (fname);
if (file.is_open()){
while (!file.eof()){
getline (file, line);
_arrName[index] = line;
index++;
}
file.close();
}
else{
cout << "Unable to open words.txt for function StoreLinesInStrArray(string, const char*)"
<< endl;
}
}
int MYCLASS::CountLinesOfFile (const char* fname){
int lines = 0;
string word;
ifstream file;
file.open(fname);
if (file.is_open()){
while (!file.eof()){
getline (file, word);
lines++;
}
file.close();
}
else{
cout << "Unable to open file words.txt for CountLinesOfFile (const char*)" << endl;
}
return lines;
}
void MYCLASS::CreateHiddenWord (){
Set_randWord(22); //assume we still didnt set a random word in a prev function
/*int len = Get_randWord().size();
cout << len << endl;
cout << Get_randWord() << endl;
for (int i = 0; i < len; i++){
hiddenWord[i] = '_';
cout << hiddenWord[i] << " ";
}
hiddenWord[len] = '\0'; */
//--------------------------------------------------------
lenofWord = Get_randWord().size();
cout << lenofWord << endl; //test
cout << Get_randWord() << endl; //test
for (int i = 0; i < lenofWord; i++){
hiddenWord[i] = '_';
cout << hiddenWord[i] << " "; //test
}
hiddenWord[lenofWord] = '\0';
}
void MYCLASS::PrintHiddenWord (){
for (int x = 0; x < lenofWord; x++){
cout << hiddenWord[x];
if (x != lenofWord - 1){
cout << " ";
}
}
}
void MYCLASS::Set_randWord (int maxoffset){
int randVal;
srand (time(NULL));
randVal = rand() % maxoffset;
randWord = _wordBuf[randVal];
}
string MYCLASS::Get_randWord (){
return randWord;
}
Code:
while (!file.eof()){
getline (file, word);
lines++;
}
Don't use eof.
Code:
while(getline(file, word))
{
++lines;
}
and again this
Code:
while (!file.eof()){
getline (file, line);
_arrName[index] = line;
index++;
}
file.close();
Code:
while (getline(file, line)){
_arrName[index++] = line;
}
There's also no need to call file.close() there; it will be closed when you leave the method.
Code:
void MYCLASS::Set_randWord (int maxoffset){
int randVal;
srand (time(NULL));
randVal = rand() % maxoffset;
randWord = _wordBuf[randVal];
}
Call srand() once, in main, at the start of your program.
Code:
Set_randWord(22); //assume we still didnt set a random word in a prev function
Where did this 22 come from?
-
22 is the max lines in the file, you can call CountLinesOfFile("words1.txt) as parameter of Set_randWord, the value will be the same i think
-
How can i declare an array in private area without knowing the size in elements?
-
In this case, it sounds like you want to have a std::string member.
-
Ill compare character input with the letters of the randWord, then ill be adding characters in the hiddenWord array.
edit: the size of hiddenWord array will be the lenth of randWord (how much characters the word have)
I have to declare the size of hte array after randWord is picked from the wordsBuffer.
I try with new operator but im not familiar with it and i call it only in the ctor, but not the ctor call Get_randWord() or Set_...
-
So, why don't you make hiddenWord a std::string instead of an array of char? That way, you can read the hidden word into this string variable, either by using the overloaded operator>> for input streams (if it really is supposed to be just a "word"), or by using std::getline.
-
But user will input a characters only, not word. Any new character input will be checked if exist in randWord and if so, will be added into hiddenWord element index