something's up with my substr() test

This is a discussion on something's up with my substr() test within the C++ Programming forums, part of the General Programming Boards category; I was just testing out the substr() method to parse a string's words into a string vector. I was just ...

  1. #1
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901

    something's up with my substr() test

    I was just testing out the substr() method to parse a string's words into a string vector. I was just re-creating what I saw in the book I'm reading so I would understand it, but why I'm not getting the desired effect alludes me. I also tried it with a pre-defined string but still no go.

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    
    using std::cin;
    using std::cout;
    using std::endl;
    using std::vector;
    using std::string;
    
    
    int main()
    {
        string aStr;
        string temp = "";
        vector<string> parse;
        string::size_type i, j;
        
        cout << "Type a sentence." << endl;
        getline(cin, aStr);
        i = 0;
        while(i != aStr.size())
        {
            while(isspace(aStr[i]) && i != aStr.size())
                i++;
                
            j = i;   
                 
            while(!isspace(aStr[j]) && j!= aStr.size())
                j++;
            
            temp = aStr.substr(i, j);
            if (i != j)
            {
                parse.push_back(temp);
                i = j;
            }  
        }
        
        cout << "You typed " << parse.size() << " words:" << endl;
        
        for (vector<string>::size_type i = 0; i < parse.size(); i++)
            cout << parse[i] << endl;    
    
        
        system("Pause");
        return 0;
    }
    Here's the output
    Code:
    Type a sentence.
    The Quick Brown Fox Jumped Over The Fence!
    You typed 8 words:
    The
    Quick Bro
    Brown Fox Jumpe
    Fox Jumped Over The
    Jumped Over The Fence!
    Over The Fence!
    The Fence!
    Fence!
    Press any key to continue . . .

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    The second argument in substr is not an index, it's the number of characters in the substring. Don't ask me why that is, but it's messed me up a number of times.

    So it should read:
    Code:
    temp = aStr.substr(i, j-i);
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    ....I see, thanks.

  4. #4
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    I also noticed that I didn't include the cctype header, but the isspace function, which the book said is declared in the header, works.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by indigo0086
    I also noticed that I didn't include the cctype header, but the isspace function, which the book said is declared in the header, works.
    Standard headers are allowed to include each another (However there's no requirement for this to happen) - the same is true for std::endl; which is declared in <ostream> - but most compilers #include <ostream> inside <iostream>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 02:03 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 06:28 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM
  4. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 03:57 PM
  5. Question About Linker Errors In Dev-C++
    By ShadowMetis in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2004, 09:42 PM

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