
救命啊~!哪位大侠帮看看~!
最后无法将信息存入文件,还有修改资料后之前输入的多个信息都没有了,只留下一个修改后的信息,哪位大侠帮看看应该怎么改~!!:
#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>
#include <ctime>
#include <fstream>
#define max 100
using namespace std;
struct stu //学生资料结构体
{
char name[10];
char sex[5];
int num;
char adress[20];
float x,y,z,score;
int number;
};
int count=0;
int temp=0;
class student //学生类
{
private:
stu data[max];
char start_del;
public:
void input(char *ch1,int num,char *ch2,char *ch3,float x,float y,float z); //输入
void storefile();
void loadfile();
void modify(int num); //修改
int check_num(int num) //确定没有重复学号
{
int m=0;
while(m<=count)
if(num==data[m++].num)
break;
if(m>count)
return 0;
else
return 1;
}
void show(); //输出
};
void student::input(char *ch1,int num,char *ch2,char *ch3,float x,float y,float z)
{
strcpy(data[count].name,ch1);
data[count].num=num;
strcpy(data[count].sex,ch2);
strcpy(data[count].adress,ch3);
data[count].x=x;
data[count].y=y;
data[count].z=z;
count++;
}
void student::modify(int num)
{
int n=0;
char *ch1,*ch2,*ch3;
float x,y,z;
while(n<=count)
if(num==data[n++].num)
break;
if(n>count)
{
cout<< "很抱歉,没有该学号的学生!"<<endl;
start_del='n';
getch();
}
else
{
temp=count;
count=n;
start_del='y';
cout<< "该学生的原始资料为:\n"
<<"序号\t姓名\t学号\t性别\t地址"<<"\t\t"<<"高数\t英语\t计算机\t总分"<<endl;
count=temp;
ch1=new char[];
ch2=new char[];
ch3=new char[];
cout<<"请输入新的资料:\n"
<<"请输入修改后的姓名:"<<endl;
cin>> ch1;
cout<<"请输入性别:"<<endl;
cin>> ch2 ;
cout<<"请输入地址:"<<endl;
cin>> ch3 ;
cout<<"请输入高数成绩:"<<endl;
cin>> x ;
while(x<0||x>100)
{
cout<<"请输入正确的成绩:"<<endl;
cin>>x;
}
cout<<"请输入英语成绩:"<<endl;
cin>> y ;
while(y<0||y>100)
{
cout<<"请输入正确的成绩:"<<endl;
cin>>y;
}
cout<<"请输入计算机成绩:"<<endl;
cin>> z ;
while(z<0||z>100)
{
cout<<"请输入正确的成绩:"<<endl;
cin>>z;
}
strcpy(data[n-1].name,ch1);
strcpy(data[n-1].sex,ch2);
strcpy(data[n-1].adress,ch3);
data[n-1].x=x;
data[n-1].y=y;
data[n-1].z=z;
count=n;
cout<<"该学生的新资料为:\n"
<<"序号\t姓名\t学号\t性别\t地址"<<"\t\t"<<"高数\t英语\t计算机\t总分"<<endl;
show();
cout<<"按任意键返回"<<endl;
getch();
}
}
void student::show()
{
data[count-1].score=data[count-1].x+data[count-1].y+data[count-1].z;
cout << count << "\t"
<< data[count-1].name << "\t"
<< data[count-1].num << "\t"
<< data[count-1].sex <<"\t"
<< data[count-1].adress << "\t"
<< data[count-1].x << "\t"
<< data[count-1].y << "\t"
<< data[count-1].z << "\t"
<< data[count-1].score<<endl;
}
void student::storefile()
{
ofstream ofile("student.txt",ios_base:ut);
if(!ofile)
{
cout<<"数据文件打开错误,数据存入文件失败!"<<endl;
return;
}
else
{
int i;
ofile<<"序号\t姓名\t学号\t性别\t地址"<<"\t\t"<<"高数\t英语\t计算机\t总分"<<endl;
for(i=0;i<count;i++)
{
ofile<<count++<<"\t"
<<data.name << "\t"
<<data.num << "\t"
<<data.sex << "\t"
<<data.adress << "\t"
<<data.x << "\t"
<<data.y << "\t"
<<data.z << "\t"
<<data.score <<endl;
}
ofile.close();
cout<<"学生信息已存入文件"<<endl;
}
}
void student::loadfile()
{
cout<<"从文件中读取信息并显示如下:"<<endl;
ifstream infile("student.txt",ios::in);
if(!infile)
{
cout<<"没有数据文件!"<<endl;
return;
}
else
{
char line[100];
int i;
for(i=0;i<count;i++)
{
infile.getline(line,100);
cout<<line<<endl;
}
}
infile.close();
}
void menu()
{
time_t t;
time(&t);
cout<<" →欢迎进入学生成绩管理系统←\n";
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<< " 当前系统时间是: " << ctime(&t) <<endl
<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<< " 1.输入学生资料\n"
<< " 2.添加学生资料\n"
<< " 3.修改学生信息\n"
<< " 0.退出\n"
<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
<<" 请选择你要的服务(0-3)" << endl;
}
void main()
{
student st;
char *ch1,*ch2,*ch3,chose;
int num,modify;
float x,y,z;
while(1)
{
system("cls");
menu();
cin>>chose;
switch(chose)
{
case '1':
{
st.loadfile();
}
break;
case '2':
{
ch1=new char[];
ch2=new char[];
ch3=new char[];
cout<<"请输入学生姓名:"<<endl;
cin >> ch1 ;
cout<<"请输入学号"<<endl;
cin >> num;
while(st.check_num(num))
{
cout << "输入学号重复,请重新输入" << endl;
cin >> num;
}
cout<<"请输入性别:"<<endl;
cin >> ch2 ;
cout<<"请输入地址:"<<endl;
cin>> ch3 ;
cout<<"请输入高数成绩:"<<endl;
cin>> x ;
while(x<0||x>100)
{
cout<<"请输入正确的成绩:"<<endl;
cin>>x;
}
cout<<"请输入英语成绩:"<<endl;
cin>> y ;
while(y<0||y>100)
{
cout<<"请输入正确的成绩:"<<endl;
cin>>y;
}
cout<<"请输入计算机成绩:"<<endl;
cin>> z ;
while(z<0||z>100)
{
cout<<"请输入正确的成绩:"<<endl;
cin>>z;
}
cout<<"记录添加成功!"<<endl;
st.input(ch1,num,ch2,ch3,x,y,z);
}
break;
case '3':
{
cout << "请输入你要修改的学生学号" << endl;
cin >> modify;
st.modify(modify);
}
break;
case '0':
{
st.storefile();
}
default:
{
cout << "输入错误!!!,请重新输入" << endl;
getch();
}
}
}
cout << "感谢你的使用\n" <<endl;
getch();
}