Strings

Hello! I am currently trying to figure out why my code is getting the "segmentation fault (core dumped)" error message here. Any suggestions as to why this is happening?

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
#include <iostream>
#include <string>
#include <vector>
using namespace std; 

/*Write a program that allows a user to type in tabular data similar to a CSV file, but instead of using commas a 
separtor, you should try to detect valid separators. First, let the user type in the line of tabular data. Then 
detect possible separator characters by looking through the input for non-number, non-letter, non-space characters. 
Find all of these chracters that appear on every single line, and display the user these characters to ask which 
one to use. For example, if you see input like this: Alex Allain, webmaster@cprogramming.com John Smith, 
john@nowhere.com. You should prompt the user to choose between comma, at sign, and period for the separtor. */

int main ()
{	
	vector<string>symbol_index (40); 
	string symbols = "~`!@#$%^&*()-_=+{[}]|:;?/>.<,'\\\"";
	string input = "jae.kim@jhykima, I like icecream & water, #coding."; 

	for (int j = 0; j < symbols.length (); j++)
	{
		int k = 0; 
		for (int i = input.find (symbols[j], 0); i != string::npos; input.find (symbols[j], i))
		{
			symbol_index[k] = symbols[i];
			k++;  
			i++; 
		}
	}
}


Last edited on
print out the values for i and k in your for loops. I suspect you are going past the bounds of either one of your arrays.
int i = input.find (symbols[j], 0); i != string::npos;  
i != string::npos is always true here. As int is not the type returned by find and cannot hold std::string::npos.
@MiiNiPaa

I see...

Then, how come I am able to initialize i as the value returned by the function find? And how can I compare i, which is an integer, to a non integer? And following program runs properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <map>
using namespace std; 

int main ()
{	
	 string haystack; 
	 int needle_appearances = 0;

	 haystack = "needle asdfnedaeneedleaefadfne eeedle nee dle needle"; 

	 for (int i = haystack.find ("needle", 0); i != string::npos; i = haystack.find ("needle", i))
	 {
	 	cout << "i: " << i << endl;
	 	needle_appearances++; 
	 	i++; 
	 }
	 cout << "The word needle appears " << needle_appearances << " in the Haystack."; 
}


So any suggestions as to how to fix this problem?

@mutexe

Yea...

That's the problem. I can't compile my code, so I can't print it out...
Last edited on
And how can I compare i, which is an integer, to a non integer?
It is an integer, just not int.

And following program runs properly.
Happens to work because on yor machine size of int and size_t happens to be same and implementation defined result of assigning unsigned data type to signed happens to behave as expected.

Another problem (which arised after I fixed int instead of size_t) is this. Find the difference:
1
2
for (int i = input.find (symbols[j], 0); i != string::npos; input.find (symbols[j], i))
for (int i = haystack.find ("needle", 0); i != string::npos; i = haystack.find ("needle", i))
symbol_index.push_back (k) inserts k into vector. That is all. it does not returns anything. Actually it returns nothing. So your expression evaluates to void = i; which is clearly wrong.
@MiiNiPaa

Thank you so much for taking the time to understand my code and help me with it. I understand now that you explained it.

Best regards,
Jae Kim
Topic archived. No new replies allowed.