Sign in to follow this  

code from my book

This topic is 4057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why doesnt this code work? It compiles.. but under runtime I get a error.. the "report this problem" error. The code is from a book.. It should be correct from the book..
#include <string>
#include <iostream>
#include <cctype>
#include <vector>

using std::cout;
using std::cin;
using std::string;
using std::vector;
using std::endl;

vector<string> split(const string& s)
{
    vector<string> ret;
    typedef string::size_type string_size;
    string_size i = 0;
    
    while (i != s.size()) {
          while (i != s.size() && isspace(s[i]))
                ++i;
          
          string_size j = i;
          
          while (j != s.size() && !isspace(s[j]))
                ++j;
          
          if (i != j) {
             ret.push_back(s.substr(i, j - i));
             i = j;
          }
    }               
}

int main()
{
    //ask for and stores a string in s1.
    cout << "Pleas enter a string:";
    string s;
    
    while (getline(cin, s)) {
          vector<string> v = split(s);
          
          for (vector<string>::size_type i = 0; i != v.size(); ++i)
              cout << v[i] << endl;
    }
    return 0;
}


Share this post


Link to post
Share on other sites
Since this is C++ if you are getting runtime error most likely your program is accessing memory it shouldn't be.
Also you should use the exact compiler mentioned in your book since even slight deviations will cause another compiler to act completely different with the same code since not all compilers comply exactly with the C++ standard.

Share this post


Link to post
Share on other sites
I looked at your code in Dev-C++. It looks like you need to actually return the vector you are working with in your split function. When I did that it seemed to work.

*Edit
I dont get the error anymore, but you seem to still have a problem with your input stream. You dont need your getline in a while loop, it will grab the whole line until a new line if I remember right.

added

return ret;

to the end of the function.

Share this post


Link to post
Share on other sites
Well from the code you've posted it shouldn't even compile.

Your split() function has a return type of vector<string> but your funtion dosen't return anything. I assume that the variable "ret" is what is suppose to be returned.

Try adding return ret; at the end of the split function.

PS: after the change to the split function I ran your program without any problem. However you should add a way to quit the app (IE exiting the loop, not just having to hit the close button).

EDIT: beat.

Share this post


Link to post
Share on other sites
Quote:
Original post by SirSmokey
Well from the code you've posted it shouldn't even compile.

Your split() function has a return type of vector<string> but your funtion dosen't return anything. I assume that the variable "ret" is what is suppose to be returned.

Try adding return ret; at the end of the split function.


Unfortunately, such things do compile (from my experience), and many compilers have a bad habit of not including this problem in the default warnings, either. :(

Share this post


Link to post
Share on other sites
Hows about changing your isspace to std::isspace because you are including <cctype> and not <ctype.h>

or you can do
using std::isspace;

Cheers ;-)

Edit: Oops, just read it was a runtime error

I am suprised it compiled... I would have thought that all the functions would be packaged inside the std namespace and typing isspace would be outside the std namespace.

Share this post


Link to post
Share on other sites

This topic is 4057 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this