How can i get the function info_all() to work?

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


class Person{
 
 private:
  string name;
  string last_name;
  double some_num;
  double some_num2;
 
 public:

   Person(){
   name="";
   last_name="";
   some_num=0.0;
   some_num2=0.0;

  }
  
  Person(string name, string last_name, double some_num, double some_num2){
     this->name=name;
     this->last_name=last_name;
     this->some_num=some_num;
     this->some_num2=some_num2;
  }
  
  void setName(string name){this->name=name;}
  string getName(){return name;}

  void setLast(string last_name){this->last_name=last_name;}
  string getLast(){return last_name;}

  void setNum1(double some_num){this->some_num=some_num;}
  double getNum1(){return some_num;}

  void setNum2(double some_num2){this->some_num2=some_num2;}
  double getNum2(){return some_num2;}

  void name_last(){
    cout<<"Name: "<<name<<endl
        <<"Last name: "<<last_name<<endl<<endl;

  }
   
  void info(){
    name_last();
    cout<<"Num1: "<<some_num<<endl
        <<"Num2: "<<some_num2<<endl<<endl;
  }

};

class Register
{
public:
  
  
  Person prs_arr[100];
  int num_of_people;
  bool entered=false;
  Register(){num_of_people=0;}
  
  void add(Person p1){

    if(num_of_people>=100){cout<<"Register full"<<endl;}

    else{
      for(int i=0;i<num_of_people;i++){
        if(p1.getName()==prs_arr[i].getName() && p1.getLast()==prs_arr[i].getLast()){
          cout<<"This person is already entered"<<endl;
          entered=true;
        }
              
      }
    }
    
    if(entered==false){
      prs_arr[num_of_people]=p1;
      ++num_of_people;
    }

  }

  void delete_prs(Person p1){
    int index=-1;
    Person *tmp=NULL;
    for(int i=0;i<num_of_people;i++){
      if(p1.getName()==prs_arr[i].getName() && p1.getLast()==prs_arr[i].getLast()){
        index=i;
        break;
      }
    }
    
    if(index>=0){
       tmp=&prs_arr[index];
       delete tmp;
       tmp=NULL;
       prs_arr[index]=prs_arr[index+1];
       --num_of_people;
    }

  } 
  
  void info_all(){
    for(int i=0;i<num_of_people;i++){
      prs_arr[i].info();
    }
  }
  

};

int main()
{


   Person p1("NN","XX",123,212.21);
   Person p2("NN2","XX2",1212,2121.212);
   
   p1.info();
   
   Register r1;r1.add(p1);
   Register r2;r2.add(p2);
   r1.info_all();

  return 0;
}[code]
[/code]
Last edited on
What is the problem with info_all()?

By the way: line 100 is wrong. You must not call delete where you didn't previously called new for.

Actually to remove the person you need a loop on line 98 which goes from the determined index to num_of_people - 1. Thus you may initialize index (line 89) with num_of_people.
I don't get the total amount of people i just get the info of "p1". In line 100 I'm trying to delete the array by using a pointer.
Well, you add

p1 -> r1
p2 -> r2

then you print r1. So just remove r2 and use r1 instead.

In line 100 I'm trying to delete the array by using a pointer.
Yes, that's wrong.
Last edited on
and how would you do it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{


   Person p1("NN","XX",123,212.21);
   Person p2("NN2","XX2",1212,2121.212);
   
   p1.info();
   
   Register r1;
r1.add(p1);
r1.add(p2);
   Register r2;r2.add(p2);
   r1.info_all();

  return 0;
}
i understood that what i meant was how would you delete the person from the array :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  void delete_prs(Person p1){
    int index=-1;
    Person *tmp=NULL;
    for(int i=0;i<num_of_people;i++){
      if(p1.getName()==prs_arr[i].getName() && p1.getLast()==prs_arr[i].getLast()){
        index=i;
        break;
      }
    }
    
    if(index>=0){
       --num_of_people;
    for(int i=index;i<num_of_people;i++)
{
       prs_arr[i]=prs_arr[i+1];
}
    }

  }
Thx a lot I almost lost hope that someone would answer my post :D
Topic archived. No new replies allowed.