Aborted (core dump)

Alright, so I got some help earlier, that seemed to work, and now I'm experiencing problems running the code. I'm running an SSH with UNIX, and the code compiles, the code links, and then I go about creating the output file and then I get the message from the terminal: Aborted (core dump). What could be the problem here? Here's the source code (skip to the bottom function clean_entry to see what I was most recently working on)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

void clean_entry( const string& s1, string& s2 )
{
string::const_iterator start = s1.begin();
string::const_iterator finish = s1.begin();

start = find_if (s1.begin(), s1.end(),
[] (char c) -> bool { return (isalnum(c) > 0); }); //finds position to start

finish = find_if(s1.begin(), s1.end(),
[] (char c) -> bool { return (!isalnum(c) > 0); }); //finds position to end

s2 = s1.substr (*start, *finish); // purges the string of everything after punctuation

for_each (s2.begin(), s2.end(), lower); //converts all letters to lowercase

}



What are the reasons that this could be happening? Any and all help is greatly appreciated.

EDIT: I went through and started looking around, and it appears to be a problem with substr. Why is this giving me problems exactly?
Last edited on
http://www.cplusplus.com/forum/general/112111/

1
2
isalnum(c) > 0
!isalnum(c) > 0
┬┐what?

> it appears to be a problem with substr. Why is this giving me problems exactly?
RTFM
Check out the comments
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
#include <iostream>
#include <algorithm>
#include <string>
#include <cctype>
using namespace std;
void clean_entry( const string& s1, string& s2 ) {
   string::const_iterator start = s1.begin();
   string::const_iterator finish = s1.begin();

   //finds position to start
   start = find_if (s1.begin(), s1.end(), 
                    [] (char c) -> bool { return (isalnum(c) > 0); }); 

   //finds position to end
   finish = find_if(s1.begin(), s1.end(), 
                     [] (char c) -> bool { return (!isalnum(c) > 0); });

   // -------------------------------------------------------------------
   // find_if returns a iterator, not a position and substr wants a 
   // position not an iterator so convert the interator to a position.
   // When you were using *start and *finish you were using the values
   // that were present at those locations (like 't' or '2') in the 
   // string which was causing you crash.
   // --------------------------------------------------------------------
   s2 = s1.substr (start-s1.begin(), finish-start); // purges the string of everything after punctuation

   // ---------------------------------------------------------------------
   // Have to use transform because for_each is not going to change the 
   // value of the elements of the string
   // for_each (s2.begin(), s2.end(), ::tolower); //converts all letters to lowercase
   transform (s2.begin(), s2.end(), s2.begin(),::tolower); //converts all letters to lowercase

}

int main(void){

   string s1("444ThISi5ATest99$%.toSeeif This works!!"),s2;
   cout << "Before: " << s1 << endl;
   clean_entry(s1,s2);
   cout << "After: " << s2 << endl;
   return 0; 
}
$ ./a.out
Before: 444ThISi5ATest99$%.toSeeif This works!!
After: 444thisi5atest99

Thanks a ton for the help!
Topic archived. No new replies allowed.