txt file and more

so i pretty much ended writing my program and i have 2 questions.
1. i want to make all the cout<< in my program to be printed on both the program itself and in a txt file, and i wanted to ask what is the most efficient way to
do it?
2. well it isn't exactly a question but i would like a feedback on my programming
because i think i'm doing it really bad, like i don't see a reason to use a pointer or an address but i see that everybody using them.
please help me, thank you.

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
 #include <iostream>
#include "rabbit.h"
#include <string>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
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));


do{

turn();


}while(robit.size()>0);

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();
      }
      }
}


if(robit[o].mutantRabbit==true){
 for(l=0; l<robit.size(); l++){
    if ( (robit[l].mutantRabbit==true) && (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;
    }
 }
 }
 if(robit.size()==1000){
    for(l=0;l<500;l++){
        robit.erase(robit.begin()+rand()%(1000-l));
    }
    cout<<"there no food left 500 rabbits are dead"<<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;
robit.erase (robit.begin()+o);
o--;
     u++;
 }
else{ cout<<"rabbit "<<robit[o].name<<" as end is turn"<<endl<<endl;}

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

}
cout<<endl<<"turn end!"<<endl<<endl;
cout<<"there are now "<<robit.size()<<" rabbits in the garden"<<endl;
cout<<"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
 #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();
void info();
      bool mutantRabbit= true;
         bool canBite=false;

        bool sex;
        string rabbitSex;
        string color;
  string rabbitColor[4];
       int rabbitAge[51];
   int age;
        string name;
        string rabbitName[21];
        int volvol;
        int i;
        int b;
        int c;
        int q;



    private:

};


#endif // RABBIT_H



rabbit.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++;
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()
{

}
1. i want to make all the cout<< in my program to be printed on both the program itself and in a txt file, and i wanted to ask what is the most efficient way to
do it?
There are several ways to do this. The easiest would be a macro (not so good, but in a small context may be ok):

1
2
3
4
5
6
#define BOUT(expr) cout << expr; fout << expr

...

for(o=0; o<c; o++ ){
        BOUT("rabbit "<< robit[o].name<< " turn"<< endl<<endl);


2. well it isn't exactly a question but i would like a feedback on my programming
because i think i'm doing it really bad, like i don't see a reason to use a pointer or an address but i see that everybody using them.
Pointer (especially raw pointer) should be avoided whenever possible. So an approach without pointer is mostly better, but you shouldn't avoid pointer at all cost...
Topic archived. No new replies allowed.