Thread: substring of a variable...

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

    substring of a variable...

    how do I get the substring of a variable?

    im trying to make a "Trivia" game...

    with scrambling words lol...

    this is my code so far...
    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 = (rand()%high_index)+low_index;
        //gets the length of the string for the while loop
        string::size_type word_length = wd[x].length();
        //begins to set subs[] to each letter of wd
        while (low_index < word_length+1)
            {
            subs[low_index] = wd[low_index];
            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();
        }
    all I want is to get substring of wd, so I can get each letter and set it to subs[low_index] then put it back together randomly
    Last edited by Mythic Fr0st; 01-17-2007 at 07:32 PM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try this:
    Code:
    string s = "abcde";
    string sub = s.substr(1, 2);
    cout<<sub<<endl;  //bc

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

    yea

    Yea that worked great, THANKS!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM
  5. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 01:30 AM