主题:求救了。请各位大侠帮忙
程序无错。在运行的时候输入的信息不见了。是不是文件操作有问题。今天就急用。情高手帮忙看一下
#include <iostream>
using namespace std;
#ifndef HEAD_H
#define HEAD_H
#include <string>
using namespace std;
//出生日期类
class birthday
{
public:
birthday();//默认构造
int get_year();//获得出生的年份
int get_month();//获得出生的月份
int get_day();//获得出生的日
void set_birth();//设置出生日期
private:
int year;
int month;
int day;
};
//学生类
class student
{
public:
student();//默认构造函数
char face();//首页
void inster_new();//新学生登记
void show_all();//显示所有学生信息
void select();//查询学生信息
void alter();//修改学生信息
void deletes();//删除学生信息
void delete_sigle();//删除一个学生记录
void delete_all();//删除所有学生记录
private:
string name;
string number;//学号
char sex;//姓别 w/m
string bornPlace;//出生地,籍贯
int age;
birthday birth;//出生日期
string address;//住址
};
#endif
#include <fstream>
#include <string>
#include <iomanip>
#include <cassert>
#include <conio.h>
//生日类构造函数
birthday::birthday()
{
year = 0;
month = 0;
day = 0;
}
//得到年份
int birthday::get_year()
{
return year;
}
//得到月份
int birthday::get_month()
{
return month;
}
//得到日
int birthday::get_day()
{
return day;
}
//设置出生日期
void birthday::set_birth()
{
cin >>year >>month >>day;
}
//学生类默认构造函数
student::student():birth()
{
name = "\0";
number = "\0";
sex = '\0';
bornPlace = " \0";
age = 0;
address = " \0";
}
//首页
char student::face()
{
char choose;
system("cls");
cout <<endl;
cout <<"\t ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 学 生 基 本 信 息 管 理 系 统 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 1. 新学生登记 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 2. 浏览所有学生信息 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 3. 查询信息 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 4. 修改信息 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 5. 删除信息 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇ 6. 关闭系统 ◇" <<endl
<<"\t || ||" <<endl
<<"\t ◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇〓◇" <<endl <<"\t\t";
choose = getch();
fflush(stdin);
return choose;
}
//新学生登记
void student::inster_new()
{
system("cls");
cout <<endl <<"\t新学生登记" <<endl <<endl;
cout <<"\t 姓名 : ";
cin >>name;
cout <<endl <<"\t 学号 : ";
cin >>number;
cout <<endl <<"\t性别(m/w) : ";
cin >>sex;
cout <<endl <<"\t 年龄 : ";
cin >>age;
cout <<endl <<"\t 出生日期 : ";
birth.set_birth();
cout <<endl <<"\t 籍贯 : ";
cin >>bornPlace;
cout <<endl <<"\t 住址 : ";
cin >>address;
fstream stuFile("students\\stu.txt", ios::app);
//assert(stuFile);
stuFile <<number <<endl;
stuFile.close();
string stuFileName = "students\\"+number+".txt";
fstream stuFile1(stuFileName.c_str());
//assert(stuFile1);
stuFile1 <<"姓名 : " <<name <<endl <<endl
<<"学号 : "<<number <<endl <<endl
<<"性别 : "<<sex <<endl <<endl
<<"年龄 : "<<age <<endl <<endl
<<"出生日期 : " <<birth.get_year() <<"." <<birth.get_month() <<"." <<birth.get_day() <<endl <<endl
<<"籍贯 : " <<bornPlace <<endl <<endl
<<"住址 : " <<address <<endl;
stuFile1.close();
cout <<endl <<endl <<"\t新生登记完成!!!!" <<endl<<endl <<"\t";
system("pause");
}
//显示所有学生信息
void student::show_all()
{
system("cls");
cout <<endl <<"所有学生信息如下 : " <<endl <<endl;
fstream stuFile("students\\stu.txt");
//assert(stuFile);
string str1;
string str2;
string str3;
cout <<setiosflags(ios::left) <<setw(15) <<"姓名 "
<<setw(15) <<"学号 " <<setw(5) <<"性别 "
<<setw(5) <<"年龄 " <<setw(15) <<"出生日期 "
<<setw(8) <<"籍贯 " <<"住址" <<endl;
while (stuFile >>number)
{
number = "students\\"+number+".txt";
fstream stuFile1(number.c_str());
//assert(stuFile1);
cout <<stuFile1.rdbuf() <<endl <<endl;
stuFile1.close();
}
stuFile.close();
cout <<endl <<endl <<"所有学生信息显示完成!!!!" <<endl <<endl;
system("pause");
}
//查询学生信息
void student::select()
{
system("cls");
cout <<endl <<"\t学生信息查询" <<endl <<endl;
cout <<"\t请输入查询学号 : ";
string no;
cin >>no;
fstream stuFile("students\\stu.txt");
//assert(stuFile);
while (stuFile >>number)
{
if (no == number)
{
cout <<endl <<number <<"的信息如下 : " <<endl;
number = "students\\"+number+".txt";
fstream stuFile1(number.c_str());
// assert(stuFile1);
cout <<endl <<stuFile1.rdbuf() <<endl <<endl;
cout <<"查询完毕!!!!" <<endl <<endl;
system("pause");
return;
}
}
}
//修改学生信息
void student::alter()
{
system("cls");
cout <<endl <<"\t信息修改" <<endl <<endl;
cout <<"\t请输入学号 : ";
string no;
cin >>no;
fstream stuFile("students\\stu.txt");
while (stuFile >>number)
{
if (no == number)
{
cout <<endl <<number <<"以前的信息如下 : " <<endl <<endl;
number = "students\\"+number+".txt";
fstream stuFile1(number.c_str());
// assert(stuFile1);
cout <<stuFile1.rdbuf() <<endl <<endl;
cout <<endl <<"请根据提示输入新的信息 : " <<endl <<endl;
cout <<"\t 姓名 : ";
cin >>name;
cout <<endl <<"\t性别(m/w) : ";
cin >>sex;
cout <<endl <<"\t 年龄 : ";
cin >>age;
cout <<endl <<"\t 出生日期 : ";
birth.set_birth();
cout <<endl <<"\t 籍贯 : ";
cin >>bornPlace;
cout <<endl <<"\t 住址 : ";
cin >>address;
string stuFileName = "students\\"+no+".txt";
ofstream stuFile2(stuFileName.c_str());
// assert(stuFile2);
stuFile2 <<"姓名 : " <<name <<endl <<endl
<<"学号 : "<<no <<endl <<endl
<<"性别 : "<<sex <<endl <<endl
<<"年龄 : "<<age <<endl <<endl
<<"出生日期 : " <<birth.get_year() <<"." <<birth.get_month() <<"." <<birth.get_day() <<endl <<endl
<<"籍贯 : " <<bornPlace <<endl <<endl
<<"住址 : " <<address <<endl;
stuFile2.close();
cout <<endl <<endl <<"\t信息修改完成!!!!" <<endl<<endl <<"\t";
system("pause");
return;
}
}
}
//删除学生信息
void student::deletes()
{
char choose;
while (1)
{
system("cls");
cout <<endl <<"\t删除学生信息" <<endl <<endl;
cout <<endl <<"\t●\t1. 删除除一个学生记录" <<endl
<<endl <<"\t●\t2. 删除所有学生记录" <<endl
<<endl <<"\t●\t3. 返回首页" <<endl <<endl <<"\t\t";
choose = getch();
fflush(stdin);
switch (choose)
{
case '1':
delete_sigle();//删除一个学生记录
break;
case '2':
delete_all();//删除所有学生信息
break;
case '3':
return;
default:
break;
}
}
}
//删除一个学生记录
void student::delete_sigle()
{
system("cls");
cout <<endl <<"\t删除学生信息" <<endl <<endl;
cout <<"\t请输入学号 : ";
string no;
cin >>no;
fflush(stdin);
ifstream stuFile("students\\stu.txt");
//assert(stuFile);
while (stuFile >>number)
{
if (no == number)
{
number = "students\\"+number+".txt";
fstream stuFile1(number.c_str());
// assert(stuFile1);
cout <<endl <<"你想删除的学生信息如下 : " <<endl <<endl;
cout <<stuFile1.rdbuf() <<endl;
stuFile1.close();
cout <<endl <<"你确定要删除吗?(y/n) : ";
if (getch() == 'y')
{
fstream stuFile2("students\\stu.txt");
// assert(stuFile2);
ofstream tempFile("students\\temp.txt");
// assert(tempFile);
while (stuFile2 >>number)
{
if (no != number)
{
tempFile <<number <<endl;
}
}
stuFile2.close();
tempFile.close();
fstream tempFile1("students\\temp.txt");
// assert(tempFile1);
fstream stuFile3("students\\stu.txt");
// assert(stuFile3);
stuFile3 <<tempFile1.rdbuf();
tempFile1.close();
stuFile3.close();
number = "students\\"+no+".txt";
fstream stuFile4(number.c_str());
stuFile4.close();
cout <<endl <<endl <<"该生信息已经删除!!" <<endl <<endl <<"\t";
system("pause");
}
break;
}
}
}
//删除所有学生记录
void student::delete_all()
{
cout <<endl <<endl <<"\t你确定在删除所有学生信息吗? 此操作不可恢复.(y/n) : ";
if (getch() == 'y')
{
fstream stuFile("students\\stu.txt");
//assert(stuFile);
while (stuFile >>number)
{
number = "students\\"+number+".txt";
fstream stuFile1(number.c_str());
//assert(stuFile1);
stuFile1.close();
}
stuFile.close();
fstream stuFile2("students\\stu.txt");
//assert(stuFile2);
cout <<endl <<endl <<"\t所有学生信息已经删除!!!" <<endl <<endl;
system("pause");
}
}
//主函数,选择功能
int main()
{
char choose;
student astu;
while (choose = astu.face())
{
switch (choose)
{
case '1':
astu.inster_new();//新生登记
break;
case '2':
astu.show_all();//显示所有学生信息
break;
case '3':
astu.select();//信息查询
break;
case '4':
astu.alter();//修改学生信息
break;
case '5':
astu.deletes();//删除学生信息
break;
case '6':
cout <<endl <<"\t\t谢谢使用!!!!!" <<endl <<endl <<"\t\t";
exit(0);
break;
default:
break;
}
}
return 0;
}