// broken_up
#include <iostream>
#include <vector>
#include <string>
usingnamespace std;
int main()
{
size_t start = 0, length;
vector<string> broken_up;
string command = "get key", temp;
string::iterator iter;
cout << "The command is: " << command << endl;
do {
for( iter = command.begin(); *iter != ' '; ++iter )
{
++length;
}
temp = command.substr( start, length );
broken_up.push_back( temp );
start = length;
} while( iter != command.end() );
cout << "\nThe command has " << broken_up.size() << "words in it." << endl;
cout << "\nThe words in the command are:";
for ( vector<string>::iterator vec_iter = broken_up.begin();
vec_iter != broken_up.end();
++vec_iter )
{
cout << endl << *vec_iter;
}
cin.get();
return 0;
}
The program produces no errors, but when it runs, it appears only for a brief second before terminating. I thought I had this solved with the cin.get() function (which has always worked for me in the past), but it doesn't change anything. I checked the 'Output' section of my compiler and it read thus:
pre-prompt
(gdb)
prompt
post-prompt
Reading symbols from Broken_up2.exe...done.
frames-invalid
pre-prompt
(gdb)
prompt
post-prompt
pre-prompt
(gdb)
prompt
post-prompt
breakpoints-invalid
Breakpoint 1 at )x401669: file (I don't want to type out the whole file name...), line 42.
pre-prompt
(gdb)
prompt
Program exited with error...
post-prompt
Then it says 'frames-invalid' a bunch of times.
My questions: (a)what does 'frames-invalid' mean, and (b)how can I fix this program so I can see the result?
Oh, I see. Thanks. Is there any way to save a value so that I can set iter to the start of the next word (i.e. where the last one left off) each iteration? And I know I can't convert 'size_t's into 'iterator's, so do you have any suggestions?
I've been working a bit with that link and a few others and have concocted a program to save the tokens in a vector of strings, which seems to work all right (as far as I can tell), but when I try to display the tokens on the screen the program shuts down. Is there some infinite loop here again that I'm just missing?
You mean I want the null character rather than NULL? I don't think so.. I pretty much just copied off this site's example of how to use strtok, so I could be wrong. I don't quite understand strtok very well yet, or why I can't print it onto the screen with the 'while' statement.
Hi sorry in my last post i didn't really pay attention it just that lots of people think that NULL == '\0'
anyway after taking a closer look at you code i think the problem is at when you initialize your iterator, if you initialize it after you have some content in your container it should work properly.
1 2 3 4 5 6
vector<string>::iterator iter = vwords.begin();
while( iter != vwords.end() )
{
cout << *iter << endl;
++iter;
}
edit: also consider using just 0(zero) instead of NULL for compatibly
Yeah, I guess I probably should use zero, but I've always thought NULL looks all important and caps-y and it adds a nice change of pace from all the lowercase letters I'm using. But whatever. Thanks for the help.