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().Code:Set_randWord(22)
is returning the picked wordCode:Get_randWord
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.
So the second code, the only difference is that the lenth variable of word is previously declared in the class.hCode: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';
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.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';
The difference i noticed is only that i changewith previously declaredCode:lenTy for reading!Code:int lenofWord



LinkBack URL
About LinkBacks


