problem with variables

This is a discussion on problem with variables within the C++ Programming forums, part of the General Programming Boards category; Code: Set_randWord(22) is picking a rand() value wich is used for rand element index from a string array, that holds ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    71

    problem with variables

    Code:
    Set_randWord(22)
    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:
    Get_randWord
    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
    Code:
    len
    with previously declared
    Code:
    int lenofWord
    Ty for reading!
    Last edited by CoffeCat; 05-12-2012 at 08:58 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,619
    Are these two independent segments of code or are they used inside the same program?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    71
    Inside the same program, but i make one of the segments as a comment so only one to be run'd.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    71
    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
    Code:
    char hiddenWord[];
    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.
    Code:
    hiddenWord
    is just a char array where will be filled with '_' characters and later replaced with letters.
    Code:
    lenofword
    is just an integer that stores the lenth of the chosen word.

    Im runing this function in a clear main() without any code.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    71
    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.
    Last edited by CoffeCat; 05-13-2012 at 12:58 AM.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    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?

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    71
    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

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    71
    How can i declare an array in private area without knowing the size in elements?

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,441
    In this case, it sounds like you want to have a std::string member.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    71
    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_...
    Last edited by CoffeCat; 05-13-2012 at 09:35 PM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,441
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    71
    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
    Last edited by CoffeCat; 05-13-2012 at 10:41 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with variables.
    By oror84 in forum C Programming
    Replies: 3
    Last Post: 05-06-2011, 07:31 AM
  2. Variables problem
    By t014y in forum C Programming
    Replies: 8
    Last Post: 09-03-2008, 04:40 PM
  3. problem with extern variables
    By crash88 in forum C Programming
    Replies: 11
    Last Post: 05-05-2006, 01:44 PM
  4. variables problem
    By zeeky in forum C++ Programming
    Replies: 15
    Last Post: 03-09-2006, 10:58 AM
  5. Problem with global variables
    By DominicTrix in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2004, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21