Data goes wrong

I had retrieve my data(teacher account detail) from .dat file, and try to edit it.
Process of edit the data is perfect and the data is been edit and show correctly.

the problem is after go through edit process, one unknow data which is a teacher account is been inserted and showing unknown variable.

here my code for edit the data
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
  int Userid;
	char UserPassword[50];
	char UserType[50];
	char UserName[50];
	int contactnumber;
	int a;
	Teacher obj;
	int found=0; 
	cout << "Please key in Teacher ID:" << endl;
	cin >> temp;
	f1.open("Teacher.dat", ios::binary | ios::in | ios::out);
	while (f1.read((char*)&obj, sizeof(obj)) && found == 0)
	{
		if (obj.getid() == temp){
			obj.Display();
			cout << "\nEnter The New Details of Teacher" << endl;
		int b = obj.getid();
		Userid = b;
		cout<<"\tInsert user Type:";
		cin>>UserType;
		cout<<"\tInsert user Password:";
		cin>>UserPassword;
		cout<<"\tInsert user Name:";
		cin>>UserName;
		cout<<"\tYr Contact Number:";
		cin>>contactnumber;
	     obj.setUser(Userid,UserPassword,UserType,UserName,contactnumber);
			int pos = (-1)*(int)sizeof(obj);
			f1.seekp(pos, ios::cur);
			f1.write((char*)&obj, sizeof(obj));
			found = 1;
		}
		}
	f1.close();
	if (found == 0){cout << "Teacher not found!!" << endl;}
	break;
	        cout<<"Please press ENTER to back to the menu ...";
			cin.ignore();
			cin.get();
}


here my display code:
1
2
3
4
5
6
7
8
9
10
11
12
13
 Teacher obj2;
          ifstream fp2;
		   cout<<"==============Teacher Listing===========\n";
          fp2.open("teacher.dat",ios::binary);
          while(fp2.read((char*)&obj2,sizeof(obj2)))
          {
                     obj2.Display();
          }
          fp2.close();
		  
		  cout<<"Please press ENTER to back to the menu ...";
			cin.ignore();
			cin.get();


here my .cpp of teacher
1
2
3
4
5
6
7
8
void User::setUser(int uid, char upss[], char ut[],char un[],int cn)
	{	Userid= uid;
	   strcpy_s(this->UserPassword,upss);
	   strcpy_s(this->UserType,ut);
	   strcpy_s(this->UserName,un);
	   contactnumber =cn;
	
	}

Topic archived. No new replies allowed.