how to maintain a database????

i just wanted to enter details of some employees and retriving them as required.The following program doesn't have any error.But when i retrive the information,it show the name and designation of the last entry.
Help me to sort this problem.

here's the 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
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<iomanip.h>
#define f 2
class staff
{
int len;
char *name;
public:
int code;
staff()
{}
staff(int c,char *n)
{
len=strlen(n);
code=c;
name=new char[len+1];
strcpy(name,n);
}
void display()
{
cout<<"________staff details____________"<<endl;
cout<<setw(10)<<"code"<<setw(10)<<code<<endl;
cout<<setw(10)<<"name"<<setw(10)<<name<<endl;
}
~staff()
{
delete name;
}
};
class teacher:public staff
{
int len1,len2;
char *sub,*pub;
public:
teacher()
{}
teacher(int c,char *n,char *s,char *p):staff(c,n)
{
len1=strlen(s);
len2=strlen(p);
sub=new char[len1+1];
pub=new char[len2+1];
strcpy(sub,s);
strcpy(pub,p);
}
void show()
{
display();
cout<<setw(10)<<"subject"<<setw(10)<<sub<<endl;
cout<<setw(10)<<"publication"<<setw(10)<<pub<<endl;
cout<<"____________________________"<<endl;
}
~teacher()
{
delete sub;
delete pub;
}
};
class officer:public staff
{
char grade;
public:
officer()
{}
officer(int c,char *n,char g):staff(c,n)
{
grade=g;
}
void show()
{
display();
cout<<setw(10)<<"grades"<<setw(10)<<grade<<endl;
cout<<"_______________________________"<<endl;
}
};
class typist:public staff
{
int speed;
public:
typist()
{}
typist(int c,char *n,int s):staff(c,n)
{
speed=s;
}
void print()
{
display();
cout<<setw(10)<<"speed"<<setw(10)<<speed<<endl;
}
};
class regular:public typist
{
public:
regular()
{}
regular(int c,char *n,int s):typist(c,n,s)
{}
void show()
{
print();
cout<<"_________________________________"<<endl;
}
};
class casual:public typist
{
float daily_wages;
public:
casual()
{}
casual(int c,char *n,int s,float dw):typist(c,n,s)
{
daily_wages=dw;
}
void show()
{
print();
cout<<setw(10)<<"daily wages"<<setw(10)<<daily_wages<<endl;
cout<<"_________________________________"<<endl;
}
};
int main()
{
clrscr();
int d,i=0,h=0,j=0,k=0,sp,dw,e,m,co;
char g,x;
char *s=new char[10];
char *p=new char[10];
char *n=new char[10];
teacher t[f];
officer o[f];
regular r[f];
casual c[f];
do
{
cout<<"wants to make entry/search 1 or 2"<<endl;
cin>>e;
switch(e)
{
case 1:
do
{
cout<<"enter details of employee(code,name)"<<endl;
cin>>co>>n;
cout<<"enter 1 for teacher"<<endl<<"2 for officer"<<endl<<
"3 for regular typist"<<endl<<"4 for casual typist"<<endl;
cin>>d;
switch(d)
{
case 1:
cout<<"enter subject and publication"<<endl;
cin>>s>>p;
t[i]=teacher(co,n,s,p);
i++;
break;
case 2:
cout<<"enter grades"<<endl;
cin>>g;
o[h]=officer(co,n,g);
h++;
break;
case 3:
cout<<"enter speed"<<endl;
cin>>sp;
r[j]=regular(co,n,sp);
j++;
break;
case 4:
cout<<"enter speed and daily wages"<<endl;
cin>>sp>>dw;
c[k]=casual(co,n,sp,dw);
k++;
}
cout<<"wants to enter more y/n";
cin>>x;
clrscr();
}while(x=='y');
break;
case 2:
do
{
cout<<"enter code and designation of employee"<<endl;
cout<<"enter 1 for teacher"<<endl<<"2 for officer"<<endl<<
"3 for regular typist"<<endl<<"4 for casual typist"<<endl;
cin>>co>>d;
switch(d)
{
case 1:
for(m=0;m<i;m++)
{
if(t[m].code==co)
t[m].show();
}
if(m==i)
cout<<"not an employee";
break;
case 2:
for(m=0;m<h;m++)
{
if(t[m].code==co)
t[m].show();
}
if(m==h)
cout<<"not an employee";
break;
case 3:
for(m=0;m<j;m++)
{
if(t[m].code==co)
t[m].show();
}
if(m==j)
cout<<"not an employee";
break;
case 4:
for(m=0;m<k;m++)
{
if(t[m].code==co)
t[m].show();
}
if(m==k)
cout<<"not an employee";
}
cout<<"wants to search more y/n";
cin>>x;
clrscr();
}while(x=='y');
}
cout<<"wants to work more y/n";
cin>>x;
clrscr();
}while(x=='y');
delete s;
delete p;
delete n;
getch();
return 0;
}


sorry for the inconvenience
Last edited on
Edit your original post and use code tags -> [code] your code here [/code]
Ok. There are more than one errors here :P

First, take a look at this:

1
2
3
4
cout<<"enter subject and publication"<<endl;
cin>>s>>p;
t[i]=teacher(co,n,s,p);
i++;

This line -> t[i]=teacher(co,n,s,p); creates a nameless, temporary teacher object (I'll call it temp), then assigns it to t[i], then destroys that temporary teacher object. You haven't defined an assignment operator for your teacher class, so the default one is used. This means that the following assignments take place:

1
2
3
4
5
6
7
t[i].len=temp.len; //<- ok
t[i].name=temp.name; //<- problem!
t[i].code=temp.code; //<- ok
t[i].len1=temp.len1; //<- ok
t[i].len2=temp.len2; //<- ok
t[i].sub=temp.sub; //<- problem!
t[i].pub=temp.pub; //<- problem! 

Pointer copying creates a problem here because these pointers are deleted right away with the destruction of temp. A solution for this would be to define your own assignment operator that actually copies the strings. Oh, and this is applied to all of your classes, not just the teacher.

Second, your program only prints one record because that's what you tell it to (assuming that code is unique for each record)

1
2
3
4
5
6
for(m=0;m<i;m++)
{
    if(t[m].code==co)
        t[m].show();
}
if(m==i) cout<<"not an employee";

Third, this here is not good design for object containers:

1
2
3
4
5
6
7
8
#define f 2

//...

teacher t[f];
officer o[f];
regular r[f];
casual c[f];

What if the user enters more than 2 teachers? Your program will most probably crash (If you tried it and it didn't crash then you were just lucky). Try the STL containers instead (vector, list, deque, etc...):

http://cplusplus.com/reference/stl/

EDIT: It would also save you a lot of typing if you used an std::string instead of char pointers to implement your strings:

http://cplusplus.com/reference/string/string/
Last edited on
thanks for the suggestion!
>Second, your program only prints one record because that's what you tell it to (assuming that code is unique for each record)

but i want to print only the required one.
i am a beginner i am not able to define my one assignment operator
so please help me out this
anushka wrote:
but i want to print only the required one.

Oh, ok. Then, I guess I didn't understand what the problem with that was...

anushka wrote:
i am a beginner i am not able to define my one assignment operator
so please help me out this

Take a look at these here:

http://www.learncpp.com/cpp-tutorial/911-the-copy-constructor-and-overloading-the-assignment-operator/

http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/

Also, note that when you overload either the assignment operator or the copy constructor it's good to overload the other one too.
Topic archived. No new replies allowed.