I need an advice for a problem

Hello there! I encountered a little problem that I cannot explain. If someone help me with solving it I will be highly appreciated.
Here's the source 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <iostream>
#include <cstring>
#include <cctype>
#include <conio.h>
using namespace std;

class ElectricalComponent {
   char *part_number;
   char *name;
   long value;
   char *unit;
public:
   ElectricalComponent( char *pn= '\0', char *n= '\0', long v= 0, char *u= '\0') {
      cout<<"\nCreated electrical component\n";
      int l;

      l= strlen( pn)+ 1;
      part_number= new char [ l];
      if(!part_number) {
         cout<< "Allocation error.\n";
         exit( 1);
      }
      strcpy( part_number, pn);

      l= strlen( n)+ 1;
      name= new char [ l];
      if(!name) {
         cout<< "Allocation error.\n";
         exit( 1);
      }
      strcpy( name, n);
      
      value= v;

      l= strlen( u)+ 1;
      unit= new char [ l];
      if(!unit) {
         cout<< "Allocation error.\n";
         exit( 1);
      }
      strcpy( unit, u);
   }

   ~ElectricalComponent() {
      delete [] part_number;
      delete [] name;
      delete [] unit;
   cout<< "deleted electrical component\n";
   }

   char *get_pn() {
      return part_number;
   }
   char *get_name() {
      return name;
   }
   long get_value() {
      return value;
   }
   char *get_unit() {
      return unit;
   }
};

class Element: public ElectricalComponent {
   char *country;
   long prize;
public:
   Element( char *pn= '\0', char *n= '\0', long v= 0, char *u= '\0', char *c= '\0', long p= 0 ): ElectricalComponent( pn, n, v, u) {
      cout<<"created element\n";
      
      int l;
      l= strlen( c)+ 1;
      country= new char [ l];
      if(!country) {
         cout<< "Allocation error.\n";
         exit( 1);
      }
      strcpy( country, c);

      prize= p;
   }

   ~Element() {
      delete [] country;
      cout<<"deleted element\n";
   }

   char *get_country() {
      return country;
   }
   long get_prize() {
      return prize;
   }
};

Element **input( Element **ptr, int &s) {
   int i;
   char part_number[20], name[ 30], unit[ 10], country[ 30];
   long value, prize;

   cout <<"\nEnter number of the electrical components: ";
   cin >>s;
   ptr= new Element *[ s];

   cout <<"\nEnter data for electrical components:\n";
   for( i= 0 ; i< s; i++) {
      cout <<"\n   Electrical Component <" <<i+1 <<">";
      cout <<"\nEnter Part Number: ";
      cin >>part_number;
      cout <<"Enter Name: ";
      cin >>name;
      cout <<"Enter Value: ";
      cin >>value;
      cout <<"Enter Unit: ";
      cin >>unit;
      cout <<"Enter Country: ";
      cin >>country;
      cout <<"Enter Prize: ";
      cin >>prize;
      ptr[ i]= new Element( part_number, name, value, unit, country, prize); 
   }
   return ptr;
}

void show( Element **ptr, int s) {
   int i;

   cout<< "\n   Information about current electrical components:\n";
   for( i= 0; i< s; i++) {
      cout <<"\nElectrical Component <" <<i+1 <<">";
      cout <<"\nPart Number: " <<ptr[ i]->get_pn() <<"\nName: " <<ptr[ i]->get_name() <<"\nValue: " <<ptr[ i]->get_value() 
          <<"\nUnit: " <<ptr[ i]->get_unit() <<"\nCountry: " <<ptr[ i]->get_country() <<"\nPrize: " <<ptr[ i]->get_prize() <<"\n";
   }
}

Element **delete_element( Element **ptr, int &s) {
   char i, j, part_number[ 20];
   char *temp;
   int status= 0;

   show( ptr, s);
   cout <<"\nEnter the part number of the element which you want to delete: ";
   cin >>part_number;
   for( i= 0; i< s; i++) {
      temp= new char[ strlen( ptr[ i]->get_pn())+ 1];
      strcpy( temp, ptr[ i]->get_pn());
      for( j= 0; j< strlen( temp); j++)
         temp[ j]= tolower( temp[ j]);
      if( strcmp( part_number, temp)== NULL) {
         for( j= i; j< s-1; j++)
            ptr[ j]= ptr[ j+1];
         ptr[ j+1]= '\0';
         s--;
         status= 1;
         cout <<"Electrical component with part number " <<part_number <<" has been deleted.";
         if( j== 0)
            ptr= '\0';
      }
      delete [] temp;
   }
   if( status== 0)
      cout <<"There is no such electrical component.\n";
   status= 0;
   return ptr;
}

void search( Element **ptr, int s) {
   int i, j, status= 0;
   char *temp;
   long up, down;

   cout<< "\nEnter a floor of the value: ";
   cin>> down;
   cout<< "Enter a limit of the value: ";
   cin>> up;
   for( i= 0; i< s; i++) {
      temp= new char[ strlen( ptr[ i]->get_name())+ 1];
      strcpy( temp, ptr[ i]->get_name());
      for( j= 0; j< strlen( temp); j++)
         temp[ j]= tolower( temp[ j]);
      if( strstr( temp, "condensator") != NULL)
         if( ( ptr[ i]->get_value() >= down) && ( ptr[ i]->get_value() <= up) ) {
            cout <<"\nPart Number: " <<ptr[ i]->get_pn() <<"\nName: " <<ptr[ i]->get_name()
<<"\nValue: " <<ptr[ i]->get_value() <<"\nUnit: " <<ptr[ i]->get_unit() 
<<"\nCountry: " <<ptr[ i]->get_country() <<"\nPrize: " <<ptr[ i]->get_prize() <<"\n";
            status= 1;
         }
      delete [] temp;
   }
   if( status== 0)
      cout <<"\nThere are no such condensators.\n";
   status= 0;
}

char *menu[]= {
   "\n<1> Input data for electrical components.",
   "<2> Output the data for the electrical components.",
   "<3> Delete electrical component.",
   "<4> Search for an condensator.",
   "<5> Exit."
};

void main() {

   int i, mode, size;

   Element **p= '\0';

   do {
      for(i= 0; i<5; i++)
         cout<< menu[ i]<< "\n";
      cout<< "\nChoose an option: ";
      cin>> mode;
      switch( mode) {
         case 1:
            p= input( p, size);
            cout <<"\nPress any key to continue.\n";
            getch();
            break;

         case 2:
            if( p)
               show( p, size);
            else 
               cout<< "\nYou haven't entered any information yet.";
            cout <<"\nPress any key to continue.\n";
            getch();
            break;

         case 3:
            if( p)
               p= delete_element( p, size);
            else 
               cout<< "\nYou haven't entered any information yet.";
            cout <<"\nPress any key to continue.\n";
            getch();
            break;

         case 4:
            if( p)
            search( p, size);
            else 
               cout<< "\nYou haven't entered any information yet.";
            cout <<"\nPress any key to continue.\n";
            getch();
            break;
      }
   } while( mode!= 5);
                  cout<< "\n CURRENT SIZE IS " <<size <<"// <-- Everything's looking OK?!? \n";   
for ( i= 0; i< size; i++) 
      delete p[ i]; 
   delete p;
}

So the problem is when performing the actions in this order: 1) Creating objects; 2) Delete an object (or more than one); 5) Exit;. When I Exit the program I recieve this: "Debug Error! DAMAGE: after Normal block (#70).......", but it still looks to work fine. Any ideas?

P.S. I have an aditional question. Can anybody help me with overloading the operator "<<", so when the user of the program Outputs the information on the screen about the objects, to save this information in a text file too?

Regards.
Last edited on
Ideas anyone :?
If you put code tags, [code] [/code] around the code the formatting will be kept intact.
BUMP
I tried the code and it's working fine for me.
If you create new objects you throw away any old objects (which are not deleted).

If you exit before having created any objects size will be uninitialized so the loop at the end can go wrong.

You have delete p; when it should be delete[] p;

In delete_element you are trying to write to the array out of bounds ptr[ j+1]= '\0';
Topic archived. No new replies allowed.