assertion failed

Hi people, I've that code.. when I do debbuging the code's Ok but message error starts up and shows me additional error:debbug assertion failed...when I remove the destructors the error disappear but the output does't show right results...
This programme is for counting dots of some string...
here's code:


#include<iostream>
#include<cstring>
#include<string>
using std::cout;
class mojetrida
{
private:
char *podretezec;
char *ps;
int citac,i,p;
int kraska;
public:
mojetrida();//,int citac,int p);
~mojetrida();
void udelej();
};
mojetrida::mojetrida()//,int citac,int p)
{
podretezec="deb.i.ll.ek";
kraska=strlen(podretezec);
podretezec=new char[];
ps=new char[kraska];
citac, p=0;
};
mojetrida::~mojetrida()
{
delete []podretezec;
delete []ps;
};
void mojetrida::udelej()
{
using std::cerr;
//int citac=0;
//int p=0;
ps=podretezec;
int krasa=strlen(ps);
for(i=0;i<krasa;i++)
{

if (ps[i]=='.')
{
++citac;
}
else if (ps[i]!='.')
{
break;
};

};

cerr<<"vystup je"<<ps<<citac;
};
int main()
{
using namespace std;
cout<<"zadejte retezec";
mojetrida obj=mojetrida();
obj.udelej();
return 0;
}
Please put your code in [code] tags: http://www.cplusplus.com/forum/articles/16853/
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
58
59
#include<iostream>
#include<cstring>
#include<string>
using std::cout;
class mojetrida
{
private
       char *podretezec;
       char *ps;
       int citac,i,p;
       int kraska;
public:
	mojetrida();
       ~mojetrida();[BR]
	void udelej();
};

mojetrida::mojetrida()
{
	podretezec="deb.i.ll.ek";
	kraska=strlen(podretezec);
	podretezec=new char[];
	ps=new char[kraska];
	citac, p=0;
};
mojetrida::~mojetrida()
{
  delete []podretezec;
  delete []ps;
};
	 void mojetrida::udelej()
	            { 
				  using std::cerr;
				  ps=podretezec;
                  int krasa=strlen(ps);
	                 for(i=0;i<krasa;i++)
	                    {
			
	                      if (ps[i]=='.')
		                     { 
				            ++citac;
		                      }
		                  else if (ps[i]!='.')
		                  {
				            break;
		                  };

	                   };
	 
					cerr<<"vystup je"<<ps<<citac;
	             };
int main()
{
	using namespace std;
	cout<<"zadejte retezec";
	mojetrida obj=mojetrida();
	obj.udelej();
	return 0;
}
Last edited on
podretezec=new char[]; this shouldn't even compile.

I suggest you to use C++ strings instead of C-style character arrays.
Also,
1
2
3
4
 else if (ps[i]!='.')
  {
      break;
  };

You don't need this code; if character is not a dot, loop will quit without counting all dots in the string

Here's the fixed code:
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
#include <iostream>
using namespace std;
class mojetrida
{
private:
string podretezec; // <---
string ps;
int citac,i,p;
int kraska;
public:
mojetrida();//,int citac,int p);
~mojetrida();
void udelej();
};
mojetrida::mojetrida()//,int citac,int p)
{
podretezec="deb.i.ll.ek";
kraska=podretezec.length(); // .length() gets the length of the string

citac=0, p=0;
};
mojetrida::~mojetrida()
{

};
void mojetrida::udelej()
{
using std::cerr;
//int citac=0;
//int p=0;
ps=podretezec;
int krasa=ps.length();
for(i=0;i<krasa;i++)
{

if (ps[i]=='.')
{
++citac;
}


};

cerr<<"vystup je "<<ps<<citac;
};
int main()
{

cout<<"zadejte retezec ";
mojetrida obj=mojetrida();
obj.udelej();
return 0;
}


Hope this helps.
You should only call delete[] if you allocated your pointer using new;

1
2
3
4
5
6
7
8
mojetrida::mojetrida()
{
	podretezec="deb.i.ll.ek"; // You should not delete[] this, you didn't allocate it using new
	kraska=strlen(podretezec);
	podretezec=new char[]; // what is this for?
	ps=new char[kraska];
	citac, p=0;
};
Last edited on
Topic archived. No new replies allowed.