Thread: problem with variables

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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, 11:58 AM
  5. Problem with global variables
    By DominicTrix in forum C++ Programming
    Replies: 6
    Last Post: 09-08-2004, 01:26 PM