somaieh_21
یک شنبه 19 آذر 1385, 10:01 صبح
سلام من در زبان c++ یک برنامه دارم راجع به وضعیت دانشجو و ... که در محیط اجرا نمی شه و مرتبا ایراد می گیره . اگه می شه لطفا کمک کنید
// this program write and read records to hard
// records Is <name student>,<number student>,<name book>,<grade>
// and has write by two class of student and university
#include<stdio.h>
#include<string.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
class student{//=================class student==================================
char name_book[10],
number_st[10],
name_st[10],
grade[5];
public:
student(){
strcpy(name_st,"\0" );
strcpy(name_book,"\0");
strcpy(number_st,"\0");
strcpy(grade,"\0" );
}
student(char *Name,char *Book,char *NU,char *GR);
char* getbook_st (){return name_book;}
char* getname_st (){return name_st; }
char* getnumber_st(){return number_st;}
char* getgrade_st (){return grade; }
void make_student(char *name,char *book,char *nu,char *gr);
void print(ostream &o);
void enter_student();
student(const student &s);
};//==========================declaration mathods===============================
student::student(char *Name,char *Book,char *NU,char *GR){
strcpy(name_st,Name );
strcpy(name_book,Book);
strcpy(number_st,NU );
strcpy(grade,GR );
}
void student::print(ostream &o){
cout.width(20);
cout.setf(ios::left);
o<<name_st<<" ";
cout.width(20);
o<<name_book<<" ";
cout.width(20);
o<<number_st<<" ";
cout.width(20);
o<<grade<<'\n';
}
ostream& operator<<(ostream &o,student &s){
s.print(o);
return o;
}
student::student(const student &s){
strcpy(name_book,s.name_book);
strcpy(number_st,s.number_st);
strcpy(name_st,s.name_st);
strcpy(grade,s.grade);
}
void student::make_student(char *name,char *book,char *nu,char *gr){
strcpy(name_st,name);
strcpy(name_book,book);
strcpy(number_st,nu);
strcpy(grade,gr);
}
void student::enter_student(){
char s[10],b[10],n[10],g[5];
cout<<"\n NAME STUDENT:";
gets(s);
strcpy(name_st,s);
cout<<"\n BOOK :";
gets(b);
strcpy(name_book,b);
cout<<"\n NUMBEER STUDENT :";
gets(n);
strcpy(number_st,n);
cout<<"\n GRADE :";
gets(g);
strcpy(grade,g);
}
//===============================class university===============================
class university{
public:
int add_student(); //add student to file
int print_student(int k=0); //print charactristics student
int search_student(char *na); //search for student
int del_student(); //deleted student
int change_student(); //chang charactristics student
};
int university::add_student(){
student s;
s.enter_student();
ofstream out("stu.dat",ios::out | ios::binary | ios::app );
if( !out ){
cout<<"\n Can not open file for add_student ";
return 0;
}
out.write((unsigned char *)&s,sizeof(student));
out.close();
return 1;
}
int university::search_student(char *na){
int k=0;
student s;
ifstream in("stu.dat",ios::in | ios::binary );
if( !in ){
cout<<"\n Can not Open file for Search_student ";
return 0;
}
while( !in.eof() ){
if( in.read((unsigned char *)&s,sizeof(student)) ){
if(strcmp(s.getnumber_st(),na)==0) {
in.close();return k;
}
k++;
}
else break;
}
in.close();
return -1;
}
int university::del_student(){
char na[10];
int k;
student s;
cout<<"\n Enter Number_STUDENT : ";
cin>>na;
k=search_student(na);
if( k<0 ){
cout<<"\n NO FIND THIS STUDENT IN UNEVERSITY ";
getch();
return 0;
}
fstream out("stu.dat",ios::out |ios::in | ios::binary );
if(!out){
cout<<"Error:open file in del_obj"<<'\n';return 0;
}
s.make_student("\0","\0","\0","\0");
out.seekp(k*sizeof(student),ios::beg);
out.write((unsigned char*)&s,sizeof(student));
out.close();
return 1;
}
int university::change_student(){
char nu[10],fild[5],ch;
cout<<" STUDENT NUMBER :";
cin>>nu;
int k=search_student(nu);
if(k<0) {
cout<<"\n THIS STUDENT NUMBER NOT FOUND IN THE UNEVERSITY ";
getch();
return 0;
}
cout<<"\nENTER FIRST CHARACTER TO CHANGE <STUDENT>,<NUMBER>,<BOOK>,<GRADE> : ";
cin>>ch;
cout<<"\nENTER FILDS NEW FOR CHANGE:";
gets(fild);
fstream in("stu.dat",ios::in |ios::out |ios::binary);
if(!in ){
cout<<"error:open file in change_student "<<'\n'; return 0;
}
student s;
in.seekg(k*sizeof(student),ios::beg);
in.read((unsigned char*)&s,sizeof(student));
switch( ch ){
case 's':
strcpy(s.getname_st(),fild);
break;
case 'g':
strcpy(s.getgrade_st(),fild);
break;
case 'b':
strcpy(s.getbook_st(),fild);
break;
case 'n':
strcpy(s.getnumber_st(),fild);
break;
default:
cout<<" can not find this case ";
getch();
}
in.seekp(k*sizeof(student),ios::beg);
in.write((unsigned char*)&s,sizeof(student));
in.close();
return 1;
}
int university::print_student(int k ){
student s;
s.make_student("\0","\0","\0","\0");
fstream in("stu.dat",ios::in| ios::out | ios::binary);
if(!in){
cout<<"can noy open file for print student "<<'\n'; return 0;
}
if(k==0){
cout<<"==========================STUDENT NUMBERS=============================== \n";
cout<<"NAME BOOK NUMBER GRADE \n";
while(!in.eof()){
if(in.read((unsigned char*)&s,sizeof(student))){
if(strcmp(s.getname_st(),"\0")==0) continue;
cout<<s;
}
else break;
}
getch();
}
else{
if(k==-1){
cout<<"\n THIS STUDENT NOT FOUND IN THE UNIVERSITY \n";
getch();
return 0;
}
in.seekg(k*sizeof(student),ios::beg);
in.read((unsigned char*)&s,sizeof(student));
cout<<s;
getch();
}
in.close();
return 1;
}
//============================void main=========================================
void main(){
university u;
char ch,ns[10];
int k;
do{
clrscr();
cout<<" <a> add____student \n";
cout<<" <p> print__student \n";
cout<<" <d> delete_student \n";
cout<<" <c> chang__student \n";
cout<<" <s> search_student \n";
cout<<" <E> stop___program \n";
cin>>ch;
switch( ch ){
case 'a':
case 'A':
k=u.add_student();
break;
case 'p':
case 'P':
k=u.print_student();
break;
case 'd':
case 'D':
k=u.del_student();
break;
case 'c':
case 'C':
u.change_student();
break;
case 's':
case 'S':
cout<<"\n Enter NUMBER_st :";
cin>>ns;
k=u.search_student(ns);
k=u.print_student(k);
break;
case 'e':
case 'E':
exit(0);
break;
default:
continue;
}
}while( 1==1 );
}
// this program write and read records to hard
// records Is <name student>,<number student>,<name book>,<grade>
// and has write by two class of student and university
#include<stdio.h>
#include<string.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
class student{//=================class student==================================
char name_book[10],
number_st[10],
name_st[10],
grade[5];
public:
student(){
strcpy(name_st,"\0" );
strcpy(name_book,"\0");
strcpy(number_st,"\0");
strcpy(grade,"\0" );
}
student(char *Name,char *Book,char *NU,char *GR);
char* getbook_st (){return name_book;}
char* getname_st (){return name_st; }
char* getnumber_st(){return number_st;}
char* getgrade_st (){return grade; }
void make_student(char *name,char *book,char *nu,char *gr);
void print(ostream &o);
void enter_student();
student(const student &s);
};//==========================declaration mathods===============================
student::student(char *Name,char *Book,char *NU,char *GR){
strcpy(name_st,Name );
strcpy(name_book,Book);
strcpy(number_st,NU );
strcpy(grade,GR );
}
void student::print(ostream &o){
cout.width(20);
cout.setf(ios::left);
o<<name_st<<" ";
cout.width(20);
o<<name_book<<" ";
cout.width(20);
o<<number_st<<" ";
cout.width(20);
o<<grade<<'\n';
}
ostream& operator<<(ostream &o,student &s){
s.print(o);
return o;
}
student::student(const student &s){
strcpy(name_book,s.name_book);
strcpy(number_st,s.number_st);
strcpy(name_st,s.name_st);
strcpy(grade,s.grade);
}
void student::make_student(char *name,char *book,char *nu,char *gr){
strcpy(name_st,name);
strcpy(name_book,book);
strcpy(number_st,nu);
strcpy(grade,gr);
}
void student::enter_student(){
char s[10],b[10],n[10],g[5];
cout<<"\n NAME STUDENT:";
gets(s);
strcpy(name_st,s);
cout<<"\n BOOK :";
gets(b);
strcpy(name_book,b);
cout<<"\n NUMBEER STUDENT :";
gets(n);
strcpy(number_st,n);
cout<<"\n GRADE :";
gets(g);
strcpy(grade,g);
}
//===============================class university===============================
class university{
public:
int add_student(); //add student to file
int print_student(int k=0); //print charactristics student
int search_student(char *na); //search for student
int del_student(); //deleted student
int change_student(); //chang charactristics student
};
int university::add_student(){
student s;
s.enter_student();
ofstream out("stu.dat",ios::out | ios::binary | ios::app );
if( !out ){
cout<<"\n Can not open file for add_student ";
return 0;
}
out.write((unsigned char *)&s,sizeof(student));
out.close();
return 1;
}
int university::search_student(char *na){
int k=0;
student s;
ifstream in("stu.dat",ios::in | ios::binary );
if( !in ){
cout<<"\n Can not Open file for Search_student ";
return 0;
}
while( !in.eof() ){
if( in.read((unsigned char *)&s,sizeof(student)) ){
if(strcmp(s.getnumber_st(),na)==0) {
in.close();return k;
}
k++;
}
else break;
}
in.close();
return -1;
}
int university::del_student(){
char na[10];
int k;
student s;
cout<<"\n Enter Number_STUDENT : ";
cin>>na;
k=search_student(na);
if( k<0 ){
cout<<"\n NO FIND THIS STUDENT IN UNEVERSITY ";
getch();
return 0;
}
fstream out("stu.dat",ios::out |ios::in | ios::binary );
if(!out){
cout<<"Error:open file in del_obj"<<'\n';return 0;
}
s.make_student("\0","\0","\0","\0");
out.seekp(k*sizeof(student),ios::beg);
out.write((unsigned char*)&s,sizeof(student));
out.close();
return 1;
}
int university::change_student(){
char nu[10],fild[5],ch;
cout<<" STUDENT NUMBER :";
cin>>nu;
int k=search_student(nu);
if(k<0) {
cout<<"\n THIS STUDENT NUMBER NOT FOUND IN THE UNEVERSITY ";
getch();
return 0;
}
cout<<"\nENTER FIRST CHARACTER TO CHANGE <STUDENT>,<NUMBER>,<BOOK>,<GRADE> : ";
cin>>ch;
cout<<"\nENTER FILDS NEW FOR CHANGE:";
gets(fild);
fstream in("stu.dat",ios::in |ios::out |ios::binary);
if(!in ){
cout<<"error:open file in change_student "<<'\n'; return 0;
}
student s;
in.seekg(k*sizeof(student),ios::beg);
in.read((unsigned char*)&s,sizeof(student));
switch( ch ){
case 's':
strcpy(s.getname_st(),fild);
break;
case 'g':
strcpy(s.getgrade_st(),fild);
break;
case 'b':
strcpy(s.getbook_st(),fild);
break;
case 'n':
strcpy(s.getnumber_st(),fild);
break;
default:
cout<<" can not find this case ";
getch();
}
in.seekp(k*sizeof(student),ios::beg);
in.write((unsigned char*)&s,sizeof(student));
in.close();
return 1;
}
int university::print_student(int k ){
student s;
s.make_student("\0","\0","\0","\0");
fstream in("stu.dat",ios::in| ios::out | ios::binary);
if(!in){
cout<<"can noy open file for print student "<<'\n'; return 0;
}
if(k==0){
cout<<"==========================STUDENT NUMBERS=============================== \n";
cout<<"NAME BOOK NUMBER GRADE \n";
while(!in.eof()){
if(in.read((unsigned char*)&s,sizeof(student))){
if(strcmp(s.getname_st(),"\0")==0) continue;
cout<<s;
}
else break;
}
getch();
}
else{
if(k==-1){
cout<<"\n THIS STUDENT NOT FOUND IN THE UNIVERSITY \n";
getch();
return 0;
}
in.seekg(k*sizeof(student),ios::beg);
in.read((unsigned char*)&s,sizeof(student));
cout<<s;
getch();
}
in.close();
return 1;
}
//============================void main=========================================
void main(){
university u;
char ch,ns[10];
int k;
do{
clrscr();
cout<<" <a> add____student \n";
cout<<" <p> print__student \n";
cout<<" <d> delete_student \n";
cout<<" <c> chang__student \n";
cout<<" <s> search_student \n";
cout<<" <E> stop___program \n";
cin>>ch;
switch( ch ){
case 'a':
case 'A':
k=u.add_student();
break;
case 'p':
case 'P':
k=u.print_student();
break;
case 'd':
case 'D':
k=u.del_student();
break;
case 'c':
case 'C':
u.change_student();
break;
case 's':
case 'S':
cout<<"\n Enter NUMBER_st :";
cin>>ns;
k=u.search_student(ns);
k=u.print_student(k);
break;
case 'e':
case 'E':
exit(0);
break;
default:
continue;
}
}while( 1==1 );
}