code from my book

Started by
5 comments, last by Nanook 17 years, 5 months ago
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;
          
          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 << endl;
    }
    return 0;
}


Advertisement
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.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
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.
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.
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. :(
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.
aha.. return ret; .. probably forgot to write it of the code in the book.. thanks! I'll try it when I get back from work!

I use dev-c++

This topic is closed to new replies.

Advertisement