Thread: substrings and loops, messing up real bad!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    43

    substrings and loops, messing up real bad!

    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 players_points;
        for (int t = 0; t < 51; t++)
            {
            subs_has_set[t] = false;
            }
        int x; int i;
        bool cont = true;
        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();
        
        cout<<"0,1 = "<<wd[x].substr(0,1)<<"\n";
        //begins to set subs[] to each letter of wd
        cout<<"wd[x] == "<<wd[x]<<","<<"\n"<<"Length = "<<word_length<<"\n"<<"\n";
        while (low_index-1 < word_length)
            {
            subs[low_index] = wd[x].substr(low_index-1, low_index);
            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)+1;
            if (subs_has_set[i] == false)
                {
                scrambledword = scrambledword + subs[i]; 
                subs_has_set[i] == true;   
                cout<<"subs[i], "<<subs[i];       
                }
            string::size_type new_word_length = scrambledword.length();
            if (new_word_length == word_length)
                {
                cont == false; 
                } 
            cin.get();
            }
        cout<<"Word to unscramble is "<<scrambledword<<", Good luck "<<"\n";
        getline(cin, players_word);
        if (players_word == wd[x])
            {
            players_points = players_points + 1;
            cout<<"Correct, +1 Point";
            }
        cin.get();
        }
    the red part, is suppost to loop through each letter of the word, and add it to subs[low_index]but when I display it, it ends up

    - updated with latest code

    please help!


    I did this

    subs[low_index] = wd[x].substr(low_index-1, low_index);

    and got

    subs [1] = h
    subs [2] = el
    subs [3] = llo
    subs [4] = lo
    subs [5] = o

    which is closer
    Last edited by Mythic Fr0st; 01-17-2007 at 09:01 PM.

Popular pages Recent additions subscribe to a feed