PDA

View Full Version : اشکال در run برنامه



ngrrezaei
دوشنبه 04 دی 1391, 23:04 عصر
با سلام به همه ی دوستان خوب برنامه نویس من یک کد نوشتم که اسم و فامیل و بقیه مشخصات دانشجو رو می گیره انها رو توی یک فایل ذخیره می کنه و delete می کنه و به روز می کنه .اما این برنامه کار نمی کنه نمی دونم اشکالش کجاست اگه می شه راهنماییم کنید .:گریه::گریه:


#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include<stdlib.h>
using namespace std;

class person
{

private:
char F_name[100];
char L_name[100];
char gender[100];
char N_code[100];
char B_place[100];
char day [100];
char month[100];
char year [100];
char day_m [100];
char month_m[100];
char year_m [100];
char day_f [100];
char month_f[100];
char year_f [100];
char F_name_f[100];
char F_name_m[100];
char L_name_m[100];
char N_code_f[100];
char N_code_m[100];
char B_place_f[100];
char B_place_m[100];
int reserved;
int id;

public:

char *g();{return L_name;}
void search();
void add();
void delet();
void search();
void edit();
void import();
void Export();
void show();
friend istream &operator>>(istream &in,person &s);
friend ostream &operator<<(ostream &out,person &s);
}
st;


//---------------------------------------------------------
istream &operator>>(istream &in,person &s)
{
cout<<"Enter person name , last name , number code,gender,Place of birth,day of birth,month of birth,year of birth"
<<" father name,mother name,mothar last name,father number code,mother number code:"<<endl;
in>>s.F_name>>s.L_name>>s.gender>>s.N_code>>s.B_place>>s.day>>s.month>>s.year
>>s.F_name_f>>s.F_name_m>>s.L_name_m>>s.N_code_f>>s.N_code_m;
return in;
}
ostream &operator<<(ostream &out,person &s){
out<<s.F_name<<"|";
out<<s.L_name<<"|";
out<<s.N_code<<"|";
out<<s.gender<<"|";
out<<s.B_place<<"|";
out<<s.day<<"|";
out<<s.month<<"|";
out<<s.F_name_f<<"|";
out<<s.F_name_m<<"|";
out<<s.L_name_m<<"|";
out<<s.N_code_f<<"|";
out<<s.N_code_m<<"|";
out<<s.B_place_f<<"|";
out<<s.B_place_m<<"|";
out<<s.day_f<<"|";
out<<s.month_f<<"|";
out<<s.year_f<<"|";
out<<s.day_m<<"|";
out<<s.month_m<<"|";
out<<s.year_m<<"|";
out<<"--------------------------------------"<<endl;
return out;
}
//-----------------------------------------------------------------------------

int main()
{
int h;
for(;;){
cout<<" 1- add :\n";
cout<<" 2- search :\n";
cout<<" 3- delete :\n";
cout<<" 4- show :\n";
cout<<" 5- exit :\n";
cout<<" Enter your selection :";
cin>>h;
if(h==1)
st.add();
if(h==4)
st.show();
if(h==2)
st.search();
if(h==3)
st.delet();
if(h==5)
exit(1);
getch();
return 0;
}

//-------------------------------------------------------------------
void add(){ ofstream file("person.dat",ios::out |ios::binary |ios::app);
if(!file){
cout<<"CAN NOT OPEN FILE .";
getch();
exit(0);}
while(1){
char b;
cin>>st;
file.write((char *)&st,sizeof(class person));
cout<<"Do you want to continue :(y/n)";
cin>>b;
if(b=='n')
break;
else
continue;
}
getch();
file.close();}
//------------------------------------------------------------------------------
void show(){ifstream f("person.dat",ios::in);
while( f.read((char *)&st,sizeof(class person))){
cout<<st;}
f.close();}
//---------------------------------------------------------------------------------
void search(){ char ans[21];
cout<<"Enter name for search :"<<endl;
cin>>ans;
ifstream f1("person.dat",ios::in);
int k=7;
while( f1.read((char *)&st,sizeof(class person))){
int p;
p=strcmp(st.g(),ans);
if(p==0){
cout<<st;
k=1;}
}
if(k==7)
cout<<"NOT FOUND...."<<endl;
f1.close();}
//----------------------------------------------------------------------------
void Delete(){
ifstream f3("student.dat",ios::binary|ios::in);
ofstream f4("temp.dat",ios::out|ios ::binary|ios::app);
char ans[21];
cout<<"Enter name to delete:"<<endl;
cin>>ans;
int k=3;
while( f3.read((char *)&st,sizeof(class person))){
if(strcmp(st.g(),ans)==0)
{
cout<<st;
k=1;
cout<<"Delete Done....\n";}

if(strcmp(st.g(),ans)==1||strcmp(st.g(),ans)==-1) {

f4.write((char *)&st,sizeof( class person));
}
}//end of while
if(k==3)
{
cout<<"Name not exist.....\n"; }
f3.close();
f4.close();
remove("student.dat");
rename("temp.dat","student.dat");}

//-------------------------------------------------------------