c++如何做保存和读取呢!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!急需啊~~
[code=C/C++][/code]
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
# define Min 5
class cjh
{
long num;
int i;
string name;
char sex;
float math;
float english;
float sport;
float Cplus;
float sum;
float ave;
public:
static int total;
public:
void Creat();
void Output();
bool Lookup(long num);
void Modify(long num);
void Add();
bool Statistic(long num);
void Statistics();
};
int cjh::total=0;
void cjh::Creat()
{
cout<<"欢迎进入学生信息建立系统!!"<<endl;
cout<<"学号 "<<"姓名 "<<"性别 "<<"高等数学 "<<"大学英语 "<<"体育 "<<"c++面向对象程序设计 "<<endl;
cin>>this->num>>name>>sex>>math>>english>>sport>>Cplus;
sum=math+english+sport+Cplus;
ave=(math+english+sport+Cplus)/4;
cjh::total++;
}
void cjh::Output()
{
cout<<"学号 "<<"\n"<<"姓名 "<<"\n"<<"性别 "<<"\n"<<"高等数学 "<<"\n"<<"英语 "<<"\n"<<"大学体育"<<"\n"<<"c++面向对象程序设计"<<endl;
cout<<num<<"\n"<<name<<"\n"<<sex<<"\n"<<math<<"\n"<<english<<"\n"<<sport<<"\n"<<Cplus<<"\n"<<endl;
}
bool cjh::Lookup(long num)
{
if(this->num==num)
return true;
else return false;
}
void cjh ::Modify(long num)
{
cout<<"该学号原来的信息是:"<<endl;
//Output();
cout<<"********************************"<<endl;
cout<<"请输入修改后的信息:"<<endl;
cout<<"学号 "<<"姓名 "<<"性别 "<<"高等数学 "<<"英语 "<<"大学体育 "<<"C++面向对象程序设计 "<<endl;
cin>>this->num>>name>>sex>>math>>english>>sport>>Cplus;
}
void cjh::Add()
{
cout<<"增加学生的信息:"<<endl;
//Output();
cout<<"**********************************"<<endl;
cout<<"学号 "<<"姓名 "<<"性别 "<<"高等数学 "<<"英语 "<<"大学体育 "<<"C++面向对象程序设计 "<<endl;
cin>>num>>name>>sex>>math>>english>>sport>>Cplus;
cjh::total++;
}
void cjh::Statistics()
{
cout<<"学号"<<" "<<"姓名"<<" "<<"性别"<<" "<<"高等数学"<<" "<<"英语"<<" "<<"大学体育"<<" "<<"C++面向程序设计"<<" "<<"总分"<<" "<<"平均分"<<endl;
cout<<" "<<num<<""<<name<<" "<<sex<<""<<math<<""<<english<<""<<sport<<""<<Cplus<<" "<<sum<<""<<ave<<endl;
}
bool cjh::Statistic( long ave)
{
if (this->ave>=ave) return true;
else
return false;
}
void welcome()
{
cout<<endl<<endl;
cout<<endl<<endl;
cout<<"\n 设计:*********************** ";
cout<<"\n *************************************************************";
cout<<"\n * 欢迎进入学生成绩管理系统 *";
cout<<"\n * *";
cout<<"\n * 1 输入 2 输出 3 查询 *";
cout<<"\n * 4 修改 5 增加 6 保存 *";
cout<<"\n * 7 统计 8 读取 9 退出 *";
cout<<"\n * *";
cout<<"\n *************************************************************";
cout<<"\n ";
cout<<"\n请选择你需要的选项:"<<endl;
}
int main()
{
cjh *stu=new cjh[Min];
welcome();
int choice;
while(choice!=0)
{
cin>>choice;
switch(choice)
{
case 1:
{
int n;
int i;
cout<<"你想输入多少个学生的信息呢?"<<endl;
cin>>n;
for ( i=0;i<n;i++)
{
if(cjh::total>Min)
stu=(cjh *)realloc(stu,(cjh::total+1)*sizeof(stu));
stu[cjh::total].Creat();
}
break;
case 2:
for( i=0;i<cjh::total;i++)
stu[i].Output();
break;
case 3:
long num;
int a;
//int total;
cout<<"请输入你要查询的学生号:";
cin>>num;
for(a=0;a<cjh::total;a++)
if(stu[a].Lookup(num))
{
cout<<"查找正确!"<<endl;
stu[a].Output();
break;
}
if (a>=cjh::total)
cout<<"查找错误!";
break;
case 4:
cout<<"请输入要修改的学生学号:";
cin>>num;
for(a=0;a<cjh::total;a++)
{if(stu[a].Lookup(num))
{ stu[a].Modify(num);
break;
if (a>=cjh::total)
cout<<"您要修改的学号不存在哦!";
break;
case 5:
int b;
//long num;
cout<<"增加学生的信息"<<endl;
for(b=0;b<1;b++)
{
if(cjh::total>Min)
stu=(cjh *)realloc(stu,(cjh::total+1)*sizeof(stu));
stu[cjh::total].Creat();
}
break;
case 6:
//
break;
case 7:
//int b;
long ave;
ave=85;
cout<<"进入学生的成绩信息统计系统!"<<endl;
cout<<"学生的成绩信息统计如下:"<<endl;
for (i=0;i<cjh::total;i++)
{
stu[i].Statistics();
}
cout<<"平均成绩大于等于85的学生的基本信息如下:"<<endl;
for (i=0;i<cjh::total;i++)
{
if(stu[i].Statistic(ave))
{
stu[i].Statistics();
}
}
if(i=cjh::total)
cout<<"对不起,目前没有平均成绩大于等于85分的学生";
break;
case 8:
//
break;
case 9:
break;
return 0;
default: cout<<"您输入有误,请重新来!";
}
}
welcome();
}
system("pause");
}
}
}
------解决方案--------------------
估计你不能结贴。得不到分.
[解决办法]
cin改为ifstream
cout改为ofstream
[解决办法]
[解决办法]
自己整理下逻辑,退到上层可以用break
直接跳出全部的循环的话可以用goto
例如:
for(;;;)
{
if( ...)
{
goto loop;
}
}
loop: cout<<"exit"<<endl;
[解决办法]
晕倒!建议使用轻量级数据库,或者说用desktop级别的Database