problem regarding the -dumping of stacktrace

Hi i am using eclipse as ide for c++ and have made a program to remove a particular character from a string .
The problem is that when i try to run it ,it ask me for the the character which i want to remove then after i input the character it gives this error

28 [sig] hello1 9168 open_stackdumpfile: Dumping stack trace to hello1.exe.stackdump

here is the code for my problem

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
    
#include<iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
string removeoccur(string s1,string s2 )
{
	string ns3="";
    string ns1;
    ns1=s1;
    string ns2;
    ns2=s2;
  

    unsigned   int pos=0;
   for(unsigned int i = 0; i < s1.length(); i++)
   {

   while(true){

		pos = ns2.find(s1[i]);

		if (pos == string::npos) // No more of this char
			 {
			
                            break;
			}
		else
			{
               

			ns3 = ns3.substr(0, pos) + ns3.substr(pos + 1);
			}
   }
   }

   return ns3;
}

int main()
{
	cout<<" this program removes the string "<<endl;


    string s3;

	string s2="puneet";
	string s1;
	cout<<" enter the string u want to remove "<<endl;
	cin>>s1;
	 s3=removeoccur(s1,s2);
	cout<<" the new improved string is "<<s3<<endl;
	return 0;
}



After debugging i found that error belongs in this particular lines

if (pos == string::npos) // No more of this char
{

break;
}


So can any one tell me y is this happening ?? thanks

thanks ,i got it ,but can you tell me why this problem arised and how to remedy it?
When I compiled this, I got a warning that your expression will always be false due to a limited range of your type. I changed it to size_t and the warning went away, however the problem was not resolved.

I debugged your code, it seems that your problem didn't come from what you listed. It actually came from this statement:
ns3 = ns3.substr(0, pos) + ns3.substr(pos + 1);

Do you see why? The error is an out_of_bounds error.

http://cplusplus.com/reference/string/string/substr/
http://cplusplus.com/reference/string/string/npos/

-Albatross
Last edited on
Topic archived. No new replies allowed.