why my program is crashing?

so this is my program:

main.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include "rabbit.h"
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
 vector<rabbit> robit;
int l=0;
void turn();
int o = 0;
int e;
int u=1;
int t=0;

int main(){
  srand(time(0));
for(int h =0;h<2;h++){
turn();

}

return 0;

}

void turn(){

if(robit.size()<1){

     robit.push_back(rabbit(rand(),1));t++;
     robit.push_back(rabbit(rand(),2));t++;
     robit.push_back(rabbit(rand(),3));t++;
     robit.push_back(rabbit(rand(),4));t++;
     robit.push_back(rabbit(rand(),5));t++;
}
for(o=0;o<robit.size();o++){
        if(robit[o].mutantRabbit==false){
    robit[o].canBite=true;
        }
}
const int c = robit.size();
for(o=0; o<c; o++ ){
        cout<<"rabbit "<< robit[o].name<< " turn"<< endl<<endl;
if (robit[o].mutantRabbit==false && robit[o].canBite==true )
{

     e = rand()%robit.size();

      for(l=0;l<robit.size();){
if(robit[l].mutantRabbit==false){
        if(l==robit.size()-1){ cout<<" poor "<<robit[o].name<<" there no rabbit to bite."<<endl<<endl<<endl;}
    l++;

}
else if(robit[l].mutantRabbit==true){
    while(robit[e].mutantRabbit==false ){
        e = rand()%robit.size();}

    robit[e].mutantRabbit=false;
    cout<<"rabbit "<<robit[e].name<<" mutantRabbit is "<<robit[e].mutantRabbit<<" after he was bite from rabbit "<<robit[o].name<<endl<<endl<<endl;
    l=robit.size();
      }
      }
}



 for(l=0; l<robit.size(); l++){
    if (  (robit[l].age>1) &&(robit[l].sex==true)&&(robit[o].sex==false) && (robit[o].age>1)){
        cout<< endl<<"rabbit " <<robit[l].name<< " and rabbit "<<robit[o].name<<" made a baby rabbit."<<endl<<endl;
           l=robit.size();
           t++;
        robit.push_back(rabbit(rand(),t));
        robit[l].age = 0;
        robit[l].color =  robit[o].color;
        cout<<" color: "<<robit[l].color<<endl;
        cout<<" age: "<<robit[l].age<<endl<<endl<<endl;
    }

 }
robit[o].ageing();
  if (robit[o].age>10 && robit[o].mutantRabbit==true){

        cout<<"rabbit "<<robit[o].name<<" is dead."<<endl<<endl;
robit.erase (robit.begin()+o);
 o--;
     u++;
 }
 else if (robit[o].age>50 && robit[o].mutantRabbit==false){

        cout<<"rabbit "<<robit[o].name<<" is dead."<<endl<<endl;

     u++;
 }
else{ cout<<"rabbit "<<robit[o].name<<" as end is turn"<<endl<<endl;}

if(o==c-u){u=1;
        o=robit.size();
}

}
cout<<endl<<"turn end!"<<endl<<endl;
cout<<"all the rabbits are:"<<endl;
for(l=0;l<51;l++){
for(o=0;o<robit.size();o++){
    if(robit[o].age==l){
        robit[o].info();
    }
}
}
}






rabbit.h
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
  #ifndef RABBIT_H
#define RABBIT_H
#include <string>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

class rabbit
{
    public:
        rabbit(int r,int p);

       void ageing();
      void go();
        virtual ~rabbit();



        bool sex;
        string rabbitSex;
        string color;
  string rabbitColor[4];
       int rabbitAge[51];
   int age;
        string name;
        string rabbitName[21];
        bool mutantRabbit= true;
        int volvol;
        int i;
        int b;
        int c;
        int q;
        bool canBite=false;
        void info();

    private:

};


#endif // RABBIT_H




rabit.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  #include "rabbit.h"
#include <iostream>
#include <ctime>
#include <string>
#include <cstdlib>

using namespace std;
rabbit::rabbit(int r,int p)
{

 sex;
     volvol = r%2;
 if (volvol>0){
     sex = true;
     rabbitSex= "male";
 }
 else{
        sex= false;
  rabbitSex = "female";
 }

 cout<<"rabbit "<<p<<endl<<" sex: "<< rabbitSex << endl;




  c = r%21;
   string rabbitName[21] = {"Steve", "Tom", "Rabi" , "Lorence", "Carrrot", "AssJucie","Nick","Israel","Noal","Hen","Keany","Lolo","Choclte","Nor","Lili","Kely","Nofar","Matilda","Igor","Joel","Nimrod",};
  name = rabbitName[c];
 cout <<" name: "<< name << endl;

  int g = r%100;
    if (g<2){

            mutantRabbit = false;
            cout << "Bahhh im a mutant!!! "<< endl;

    }

    if(p<6){
i = r%4;
   string rabbitColor[4]= {"white", "brown", "black", "spotted"};
 color = rabbitColor[i];
 cout <<" color: "<<color<<endl;
    }
   int rabbitAge[11];
for(int m =0;m<11;m++){
    rabbitAge[m]=m;
}
 if(p<6){ b = r%11;
 age = rabbitAge[b];
 cout << " age: "<< age<< endl<<endl<<endl;
    }
}




void rabbit::ageing(){
    age+=20;
cout<<"rabbit "<<name<<" is now "<<age<< " years old."<<endl<<endl;
}
void rabbit::info(){
cout<<endl<<name<<endl<<endl<<"age: "<<age<<endl<<"color: "<<color<<endl<<"sex: ";
if(sex==true){
    cout<<"male"<<endl;
}
if(sex==false){
    cout<<"female"<<endl;
}
if(mutantRabbit==false){
    cout<<"mutant rabit"<<endl;
}
cout<<endl<<endl;
}
rabbit::~rabbit()
{

}





now i think it has something to do with the
 
robit.erase (robit.begin()+o);


in the main.cpp and with the
1
2
3
4
void rabbit::ageing(){
    age+=20;
cout<<"rabbit "<<name<<" is now "<<age<< " years old."<<endl<<endl;
}


because if i change the ageing to age++ or if i delete the robit.erase, the program runs fine.

please help me, thank you.
1
2
3
4
5
    const int c = robit.size();
    for(o=0; o<c; o++ ) {
//...
robit.erase (robit.begin()+o);
o--;
robit.size() canchange in your loop. But even if you remove some entities from your vector, you still loop on old amount of elements. Which leads to invalid vector access.
Lines
1
2
3
4
        if(o==c-u) {
            u=1;
            o=robit.size();
        }
do not help, as robit.size(); has changed and is less than c. Making next iteration of the loop come through anyway.
oh my god your right, how i didn't notice it ?
thank you very much.
Topic archived. No new replies allowed.