Thread: substrings and loops, messing up real bad!

  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.

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    subs[low_index] = wd[x].substr(low_index, low_index);
    Low to low? That'll give you a string with a character in it, right? Or perhaps nothing at all?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Low to low? That'll give you a string with a character in it, right?
    No...

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Oh, nevermind.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

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

    hmm

    so in c++ substr "hello" 1,1 = (nothing)

    ?

    weird i'll try

    low_index, low_index+1

    its now

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

    hmm?

    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 08:51 PM.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    string subs[50];
    string wd[50];
    
    wd[4] = "hello";
    string::size_type word_length = wd[4].length();
    
    int low_index = 0;
    while (low_index < word_length)
    {
        subs[low_index] = wd[4][low_index];  
        cout<<"subs["<<low_index<<"] == "<<subs[low_index]<<"\n"<<"\n";
        
    	low_index++;
    }
    If you have a string like this:

    string str = "hello";

    You can access any character using the syntax str[2], which in this case would be the 3rd character(because arrays start at index 0).

    In the example above wd[4] is a string, so its characters can be accessed using the same syntax: wd[4][2], where wd[4] is the name of the string and [2] accesses the 3rd character.

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

    omg

    OMG, thanks alot

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    so in c++ substr "hello" 1,1 = (nothing)
    You should really get a book and/or find a website to look up the definitions of C/C++ functions. You can't just use a function like substr() and not know what arguments it expects and how substr() uses those arguments. The first number is the start position in the string, and the second number is the length. Therefore, in the special case when you use 1 for both arguments, then substr(1, 1) returns 1 character. However, more generally if you write substr(3,3), then substr() returns 3 characters(if there are 3 characters in the string past index position 3).

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

    hmm

    confusing, I do have a index thing manual open, yes I'll get a book, (no chances to go to library yet! i cant drive lol!

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    hmm
    confusing,
    Let's try it another way. If you write:

    string myStr = "hello world";
    string result = myStr.substr(2,2);

    you are asking the substr() function to copy some characters from myStr into result. The first argument says to start copying at index position 2 in myStr, and that is the first 'l'.

    The second argument says to copy 2 characters in total. So, result will contain 'll'.
    Last edited by 7stud; 01-18-2007 at 01:53 AM.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    43
    ohhh, I see

    its different from what I have learnt (other languages)

    so

    string m = "hello"
    string res = m.substr(2,1)
    being 'l' and only 1 character....

    so

    I see! thanks

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (int t = 0; t < 51; t++)
    This should be
    for (int t = 0; t < 50; t++)

    > while (cont == true)
    You shouldn't really compare boolean variables for true / false, since they already are true / false
    while ( cont )
    should suffice.

    > cont == false;
    Now this should have been
    cont = false;

    A good compiler would have warned you about a useless comparison, did yours?
    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.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    43
    no, my compiler is dev - C++

    lol

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    In the project settings, you can add additional flags

    Say
    -W -Wall -ansi -pedantic -O2
    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.

Popular pages Recent additions subscribe to a feed