Program runs then terminates before the result can be seen

I wrote a program to divide a string phrase into separate words by using the spaces in the string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// broken_up

#include <iostream>
#include <vector>
#include <string>

using namespace 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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?
1
2
3
4
5
6
7
8
9
10
11
   do {
        for( iter = command.begin(); *iter != ' '; ++iter )
        {
         ++length;
        }
        
        temp = command.substr( start, length );
        broken_up.push_back( temp );
        
        start = length;
        } while( iter != command.end() );


This an infinite loop on any situation where command contains a space.

1
2
3
4
        for( iter = command.begin(); *iter != ' '; ++iter )
        {
         ++length;
        }

Your reseting iter to the start of your string. So it'll never reach command.end();
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?
Last edited on
Firstly,
http://www.cplusplus.com/reference/clibrary/cstring/strtok.html

Secondly.
string.find() is a function you should get familiar with.


If you really have the desire to re-invent the wheel and write your own the hard way.
1
2
3
4
5
6
7
8
9
10
11
12
iter = command.begin();

while (iter != command.end()) {
  while((iter != ' ') && (iter != command.end()) {
   iter++;
   length++;
  }

  temp = // etc

 iter++;
}
Wow, that link is perfect! I had no idea such a thing existed. Thanks for showing me.
No worries :)
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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// c_str + strtok

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    char * pcstr, * ptokens;
    string str;
    vector<string> vwords;
    vector<string>::iterator iter = vwords.begin();
    
    cout << "Please enter a string: " << endl;
    getline( cin, str );
    
    // pcstr points to the first element in an array of chars
    pcstr = new char [ str.size() + 1 ];
    // copy c-string version of str to pstr
    strcpy( pcstr, str.c_str() );
    
    ptokens = strtok( pcstr, " .,!?" );
    // save ptokens in a vector of strings
    while( ptokens != NULL )
    {
        vwords.push_back( ptokens );
        ptokens = strtok( NULL, " .,!?" );
    }
    
    while( iter != vwords.end() )
    {
        cout << *iter << endl;
        ++iter;
    }
    
    delete [] pcstr;
    cin.get();
    return 0;
}


In most systems NULL = 0;
I'm guessing you want the null character?
 
'\0' // back slash zero 
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

Jeff
Last edited on
Wow, you're suggestion worked perfectly! Thanks!

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.
Topic archived. No new replies allowed.