Thread: substring of a variable...

  1. #1
    Registered User
    Join Date
    Jan 2007

    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...
    #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
        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];
        //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);
    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
    Try this:
    string s = "abcde";
    string sub = s.substr(1, 2);
    cout<<sub<<endl;  //bc

  3. #3
    Registered User
    Join Date
    Jan 2007


    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