![]() |
#2
rjsp2013-07-31 10:08
|

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Student{
private:
string NUM;
string NAME;
double SCORE;
friend void OUTPUT();
public:
Student(){}
Student(string num,string name,double score){
NUM = num;
NAME = name;
SCORE = score;
}
void setStudent(string num,string name,double score){
NUM = num;
NAME = name;
SCORE = score;
}
void display(){
cout<<NUM<<"\t"<<NAME<<"\t"<<SCORE<<endl;
}
double getSCORE(){return SCORE;}
string getNUM(){return NUM;}
};
void WELCOM(){
cout<<"***************************************"<<endl;
cout<<"***************************************"<<endl;
cout<<"** **"<<endl;
cout<<"** **"<<endl;
cout<<"**-------------欢迎使用--------------**"<<endl;
cout<<"** **"<<endl;
cout<<"** **"<<endl;
cout<<"***************************************"<<endl;
cout<<"***************************************"<<endl;
cout<<"添加学生信息----add"<<endl;
cout<<"查看学生信息----look"<<endl;
cout<<"退出系统----exit"<<endl;
}
void INPUT(){
int n;
string num,name;
double score;
cout<<"请输入这次要存入学生的人数:";
cin>>n;
Student stu[n];
ofstream ofile("student.dat",ios::app|ios::binary);
ofile.seekp(0,ios::end);
for(int i=0;i<n;i++){
cout<<"编号:";cin>>num;
cout<<"姓名:";cin>>name;
cout<<"成绩:";cin>>score;
stu[i].setStudent(num,name,score);
ofile.write((char *)&stu[i],sizeof(stu[i]));
}
cout<<"已存入这"<<n<<"个学生的信息,请继续操作..."<<endl;
ofile.close();
}
void OUTPUT(){
char bol;
string num;
Student temp;
ifstream ifile("student.dat",ios::in|ios::binary);
ifile.seekg(0,ios::beg);
cout<<"按编号查询(f)或显示全部(a)"<<endl;
cin>>bol;
if(bol=='f'){
cout<<"请输入学生编号:";
cin>>num;
ifile.read((char *)&temp,sizeof(temp));
while(!ifile.eof()){
if(temp.NUM==num){temp.display();}
ifile.read((char *)&temp,sizeof(temp));
}
}
else if(bol=='a'){
ifile.read((char *)&temp,sizeof(temp));
cout<<"1"<<endl;
temp.display();
cout<<"2"<<endl;
while(!ifile.eof()){
temp.display();
ifile.read((char *)&temp,sizeof(temp));
}
}
ifile.close();
}
int main(){
WELCOM();
bool Judge=true;
string Cal;
while(Judge){
cin>>Cal;
if(Cal=="add"){INPUT();}
else if(Cal=="look"){OUTPUT();}
else if(Cal=="exit"){Judge=false;}
}
return 0;
}
#include <string>
#include <fstream>
using namespace std;
class Student{
private:
string NUM;
string NAME;
double SCORE;
friend void OUTPUT();
public:
Student(){}
Student(string num,string name,double score){
NUM = num;
NAME = name;
SCORE = score;
}
void setStudent(string num,string name,double score){
NUM = num;
NAME = name;
SCORE = score;
}
void display(){
cout<<NUM<<"\t"<<NAME<<"\t"<<SCORE<<endl;
}
double getSCORE(){return SCORE;}
string getNUM(){return NUM;}
};
void WELCOM(){
cout<<"***************************************"<<endl;
cout<<"***************************************"<<endl;
cout<<"** **"<<endl;
cout<<"** **"<<endl;
cout<<"**-------------欢迎使用--------------**"<<endl;
cout<<"** **"<<endl;
cout<<"** **"<<endl;
cout<<"***************************************"<<endl;
cout<<"***************************************"<<endl;
cout<<"添加学生信息----add"<<endl;
cout<<"查看学生信息----look"<<endl;
cout<<"退出系统----exit"<<endl;
}
void INPUT(){
int n;
string num,name;
double score;
cout<<"请输入这次要存入学生的人数:";
cin>>n;
Student stu[n];
ofstream ofile("student.dat",ios::app|ios::binary);
ofile.seekp(0,ios::end);
for(int i=0;i<n;i++){
cout<<"编号:";cin>>num;
cout<<"姓名:";cin>>name;
cout<<"成绩:";cin>>score;
stu[i].setStudent(num,name,score);
ofile.write((char *)&stu[i],sizeof(stu[i]));
}
cout<<"已存入这"<<n<<"个学生的信息,请继续操作..."<<endl;
ofile.close();
}
void OUTPUT(){
char bol;
string num;
Student temp;
ifstream ifile("student.dat",ios::in|ios::binary);
ifile.seekg(0,ios::beg);
cout<<"按编号查询(f)或显示全部(a)"<<endl;
cin>>bol;
if(bol=='f'){
cout<<"请输入学生编号:";
cin>>num;
ifile.read((char *)&temp,sizeof(temp));
while(!ifile.eof()){
if(temp.NUM==num){temp.display();}
ifile.read((char *)&temp,sizeof(temp));
}
}
else if(bol=='a'){
ifile.read((char *)&temp,sizeof(temp));
cout<<"1"<<endl;
temp.display();
cout<<"2"<<endl;
while(!ifile.eof()){
temp.display();
ifile.read((char *)&temp,sizeof(temp));
}
}
ifile.close();
}
int main(){
WELCOM();
bool Judge=true;
string Cal;
while(Judge){
cin>>Cal;
if(Cal=="add"){INPUT();}
else if(Cal=="look"){OUTPUT();}
else if(Cal=="exit"){Judge=false;}
}
return 0;
}
代码如上,觉得是函数OUTPUT出了错,但是找了好久没找到错误。
错误表现为当输出学生信息时显示程序停止运行!我觉得最大的可能是ifile.read()并没有读出学生信息temp,但是,read()函数并没有错误!
理想状态下 输入look后再输入a显示存入的全部学生的信息,比如
输入a回车
101 张三 96
102 李四 97
103 王五 99
但错误状态为
输入a回车
出来对话框:程序停止运行
谢谢楼下的提醒...
[ 本帖最后由 小俎俎 于 2013-7-31 10:51 编辑 ]