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?