wd[x].length returning 1, but it is 6!

This is a discussion on wd[x].length returning 1, but it is 6! within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main () { string wd[50]; cout<<"Trivia will begin"<<"\n"<<"\n"; wd[1] = "hello"; wd[2] = ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    wd[x].length returning 1, but it is 6!

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
        {
        string wd[50];
        cout<<"Trivia will begin"<<"\n"<<"\n";
        wd[1] = "hello";
        wd[2] = "need";
        wd[3] = "help";
        wd[4] = "yell";
        wd[5] = "box";
        string subs[50];
        int low_index = 1;
        int high_index = 5;
        int lsub_index = low_index;
        int hsub_index = high_index;
        bool subs_has_set[50];
        int x; int i;
        bool cont;
        string players_word;
        string scrambledword;
        //makes a random number, to select the word we will use
        srand((unsigned)time(0));   
        x = 1; //(rand()%high_index)+low_index;
        //gets the length of the string for the while loop
        string::size_type word_length = wd[x].length(); // is 1, should be 6
        
        
        //begins to set subs[] to each letter of wd
        cout<<"wd[x] == "<<wd[x]<<","<<"\n"<<"Length = "<<x<<"\n"<<"\n";
        while (low_index < word_length)
            {
            subs[low_index] = wd[x].substr(low_index, low_index-1);
            cout<<"subs"<<low_index<<" == "<<subs[low_index]<<"\n"<<"\n";
            low_index++;
            }
        //loops so all characters are used and not overlap
        while (cont == true)
            {
            i = (rand()%word_length)+low_index;
            if (subs_has_set[i] == false)
                {
                scrambledword = scrambledword + subs[i]; 
                subs_has_set[i] == true;          
                }
            string::size_type new_word_length = scrambledword.length();
            if (new_word_length == word_length)
                {
                cont == false; 
                } 
            }
        cout<<"Word to unscramble is "<<scrambledword<<", Good luck "<<"\n";
        getline(cin, players_word);
        cin.get();
        }
    ok, i've set x = 1, instead of randomating it... so the word is "Hello", Obviously 6 letters long...

    BUT its returning 1, and I need to fix it badly

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    string wd[50];
    wd[1] = "hello";
    	
    string::size_type word_length = wd[1].length();
    cout<<word_length<<endl; //5
    By the way, arrays start at index position 0, so you should be writing:

    wd[0] = "hello";
    Last edited by 7stud; 01-17-2007 at 07:09 PM.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    ya

    thanks, obvious mistake, 5 letters O_O not six! stupid! lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stop GUI Application returning when run
    By DaveHope in forum Windows Programming
    Replies: 7
    Last Post: 06-29-2009, 08:57 PM
  2. importance of returning reference in operator overloading.
    By vaibhavs17 in forum C++ Programming
    Replies: 20
    Last Post: 05-13-2009, 12:28 PM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  4. Function returning incorrect value
    By CHurst in forum C Programming
    Replies: 3
    Last Post: 12-13-2005, 12:27 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM

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