键盘输入的信息无法写入文件中,请大神帮忙改改
源程序如下:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
//#pragma comment(lib,"ws2_32.lib")
using namespace std;
const int max = 200;
class Number
{
public:
string num;//编号
string name;//姓名
string sex;//性别"男和女"
int age;//年龄
Number(){}
Number(string num, string name, string sex, int age) :num(num), name(name), sex(sex), age(age){}
void input();
void show();
string Get_name()
{
return name;
}
string Get_num()
{
return num;
}
};//基类
class Teacher : public Number
{
private:
string system;//系部
string major;//专业
string post_T;//职称
public:
Teacher(){}
Teacher(string num, string name, string sex, int age, string system, string major, string post_T) :Number(num, name, sex, age), system(system), major(major), post_T(post_T){}
void input();//输入
void show();//输出
//string get_system(){ return system; }
//string get_major(){ return major; }
//string get_post_T(){ return post_T; }
friend istream &operator>>(istream &in, Teacher &ob);
friend ostream &operator<<(ostream &out, Teacher &ob);
};
//Teacher类
istream &operator>>(istream &in, Teacher &ob)
{
in >> ob.system >> ob.post_T >> ob.major;
return in;
}
ostream &operator<<(ostream &out, Teacher &ob)
{
out << ob.system << ob.post_T << ob.major << endl;
return out;
}
class Teacher_Manage
{
Teacher Tea[max];
int top;//记录个数f
public:
Teacher_Manage()
{
top = 1;
}
void Add();//添加
void show();//显示
void search();//查找
void Edit(){}//修改
void Delete();//删除
void Total();//统计
void save();//保存
void read();//读取
};
void Number::show()
{
ifstream in("f.txt", ios::in);
if (!in)
{
cout << "can't open it!" << endl;
return;
}
cout << "基本信息:" << endl;
cout << "姓名" << name << "学号" << num << "性别" << sex << "年龄" << age << endl;
in.close();
}
void Number::input()
{
ofstream out("f.txt", ios::out);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
cout << "录入基本信息:" << endl;
cout << "姓名" << endl;
cin >> name;
cout << "学号" << endl;
cin >> num;
cout << "性别" << endl;
cin >> sex;
cout << "年龄" << endl;
cin >> age;
out.close();
}
void Teacher::show()
{
ifstream in("f.txt", ios::in);
if (!in)
{
cout << "can't open it!" << endl;
return;
}
Number::show();
cout << "系部" << system << "专业" << major << "职位" << post_T << endl;
in.close();
}
void Teacher::input()
{
ofstream out("f.txt",ios::out);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
Number::input();
cout << "系部" << endl;
cin >> system;
cout << "专业" << endl;
cin >> major;
cout << "职位" << endl;
cin >> post_T;
out.close();
}
void Teacher_Manage::Add()
{
void Teacher_Menu();
ofstream out("f.txt",ios::out);
ifstream in("f.txt",ios::in);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
if (top >= max)
{
cout << "人员已满" << endl;
return;
}
cout << "1,输入,0,结束输入" << endl;
int a = 1;
while (a)
{
//fflush(stdin);
if (a == 1)
{
cout << "请输入你需要添加的人员信息:" << endl;
Teacher t;
t.input();
for (int i = 0; i < top; i++)
{
if (t.Get_num() == Tea[i].num)
{
cout << "该编号已存在!请重新输入:" << endl;
continue;
}
else
{
Tea[i] = t;
top=top+1;
out << top;
}
out << Tea[i].name<<Tea[i].num<<Tea[i].sex<<Tea[i].age <<t<< endl;
}
//fflush(stdin);
out.close();
//cout << "y是否继续输入,n是退出输入" << endl;
//int b = getchar();
}
cin >> a;
if (a == 0)
{
//break;
Teacher_Menu();
}
}
}
//教师的添加功能
void Teacher_Manage::search()
{
ifstream in("f.txt",ios::in);
ofstream out("f.txt",ios::out);
in >> top;
if (!in)
{
cout << "can't open it!" << endl;
return;
}
if (top == 0)
{
cout << "无人员记录!" << endl;
return;
}
Teacher_Manage tm;
Teacher t;
void Teacher_Menu();
int j = 1;
while (j)
{
if (j == 1)
{
cout << "输入查询方式:1.按编号查找\t2.按姓名查找" << endl;
int n;
cin >> n;
switch (n)
{
case 1:{
cout << "请输入要查找的编号:\n" << endl;
string num;
cin >> num;
for (int i = 0; i < top; i++)
{
if (num == Tea[i].num)
{
cout << "该人员信息如下:" << endl;
in >> Tea[i];
tm.show();
}
}
}
break;
case 2:{
cout << "请输入要查找的姓名:" << endl;
string name;
cin >> name;
for (int i = 0; i < top; i++)
{
if (name == Tea[i].name)
{
cout << "该人员信息如下:" << endl;
in >> Tea[i];
//in >> Tea[i].name >> Tea[i].num >> Tea[i].sex >> Tea[i].age >> t;
tm.show();
}
}
}
break;
default:cout << "查无此人!" << endl; break;
}
}
cin >> j;
}
Teacher_Menu();
in.close();
}
//教师人员查询
void Teacher_Manage::show()
{
void Teacher_Menu();
Teacher t;
ifstream in("f.txt",ios::in);
if (!in)
{
cout << "can't open it!" << endl;
return;
}
if (top == 0)
{
cout << "无数据!" << endl;
return;
}
in >> top;
for (int i = 0; i < top; i++)
{
cout << "第i个人员的信息:" << i << endl;
in >> Tea[i];
//in >> Tea[i].name>>Tea[i].num>>Tea[i].sex>>Tea[i].age>>t;
//cout << Tea[i].name << Tea[i].num << Tea[i].sex << Tea[i].age << t;
Tea[i].show();
}
Teacher_Menu();
in.close();
}
//教师显示
void Teacher_Manage::Delete()
{
void Teacher_Menu();
ofstream out("f.txt",ios::out);
ifstream in("f.txt", ios::in);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
if (top == 0)
{
cout << "无数据可操作" << endl;
return;
}
cout << "选择删除方法:" << endl;
cout << "1.编号" << "2.姓名" << endl;
switch (int x = getchar())
{
case 1:
{cout << "请输入要删除的编号" << endl;
string num1;
cin >> num1;
for (int i = 0; i < top; i++)
{
in >> Tea[i].num;
if (num1 == Tea[i].num)
{
for (int j = i; j < top - 1; j++)
{
Tea[j] = Tea[j + 1];
top--;
out << Tea[j] << top << endl;
}
}
else
{
cout << "无此人信息" << endl;
}
}
}
break;
case 2:
{
cout << "请输入要删除的姓名:" << endl;
string name;
cin >> name;
for (int i = 0; i < top; i++)
{
in >> name;
if (name == Tea[i].name)
{
for (int j = i; j < top - 1; j++)
{
Tea[j] = Tea[j + 1];
top--;
out << Tea[j] << top << endl;
}
}
else
{
cout << "无此人信息" << endl;
}
}
}
break;
default:break;
}
Teacher_Menu();
out.close();
in.close();
}
//Teacher人员信息删除操作
void Teacher_Manage::Total()
{
void Teacher_Menu();
ofstream out("f.txt",ios::out);
ifstream in("f.txt",ios::in);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
cout << "1.按人数统计 2.按男女统计" << endl;
int a = 0, b = 0;
in >> top;
cin >> a;
switch (a)
{
case 1: cout << "共有" << top << "个" << endl; break;
case 2:{
string s = "男";
string n = "女";
for (int i = 0; i < top; i++)
{
in >> Tea[i].sex;
if (s == Tea[i].sex || n == Tea[i].sex)
{
a++;
b++;
out << a << b;
}
}
cout << "男生有" << a << "个" << "\n" << "女生有" << b << "个" << endl;
}
}
Teacher_Menu();
out.close();
in.close();
}
//Teacher人员信息统计操作
void Teacher_Manage::save()
{
Teacher t;
void Teacher_Menu();
ofstream out("f.txt", ios::out);
ifstream in("f.txt", ios::in);
if (!out)
{
cout << "can not!" << endl;
return;
}
in >> top;
for (int i = 0; i < top; i++)
{
while (cin >> Tea[i].name >> Tea[i].num >> Tea[i].sex >> Tea[i].age >> t)
{
out << Tea[i].name << Tea[i].num << Tea[i].sex << Tea[i].age << t << endl;
}
}
Teacher_Menu();
out.close();
}
void Teacher_Manage::read()
{
ifstream in("f.txt", ios::in);
void Teacher_Menu();
Teacher t;
if (!in)
{
cout << "打开失败!" << endl;
return;
}
int i = 0;
in >> top;
for (; i < top; i++)
{
while (in >> Tea[i].name >> Tea[i].num>> Tea[i].sex >> Tea[i].age >> t)
{
//Tea[i].show();
cout << Tea[i].name << Tea[i].num << Tea[i].sex << Tea[i].age << t << endl;
i++;
top++;
}
}
Teacher_Menu();
in.close();
}
//
void Teacher_Menu()
{
Teacher_Manage T_M;
void firstmenu();
cout << "请输入你的选择:" << endl;
int n;
cout << "\t\t" << "1.添加" << "\t" << "\t" << endl;
cout << "\t\t" << "2.显示" << "\t" << "\t" << endl;
cout << "\t\t" << "3.查询" << "\t" << "\t" << endl;
cout << "\t\t" << "4.编辑" << "\t" << "\t" << endl;
cout << "\t\t" << "5.删除" << "\t" << "\t" << endl;
cout << "\t\t" << "6.统计" << "\t" << "\t" << endl;
cout << "\t\t" << "7.保存" << "\t" << "\t" << endl;
cout << "\t\t" << "8.读取" << "\t" << "\t" << endl;
cout << "\t\t" << "9.black" << "\t" << "\t" << endl;
cin >> n;
switch (n)
{
case 1:T_M.Add(); break;
case 2:T_M.show(); break;
case 3:T_M.search(); break;
case 4:T_M.Edit(); break;
case 5:T_M.Delete(); break;
case 6:T_M.Total(); break;
case 7:T_M.save(); break;
case 8:T_M.read(); break;
//case 9:firstmenu(); break;
default:
break;
}
//cout << "结束请按0!" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Teacher_Menu();
system("pause");
return 0;
}
源程序如下:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>
//#pragma comment(lib,"ws2_32.lib")
using namespace std;
const int max = 200;
class Number
{
public:
string num;//编号
string name;//姓名
string sex;//性别"男和女"
int age;//年龄
Number(){}
Number(string num, string name, string sex, int age) :num(num), name(name), sex(sex), age(age){}
void input();
void show();
string Get_name()
{
return name;
}
string Get_num()
{
return num;
}
};//基类
class Teacher : public Number
{
private:
string system;//系部
string major;//专业
string post_T;//职称
public:
Teacher(){}
Teacher(string num, string name, string sex, int age, string system, string major, string post_T) :Number(num, name, sex, age), system(system), major(major), post_T(post_T){}
void input();//输入
void show();//输出
//string get_system(){ return system; }
//string get_major(){ return major; }
//string get_post_T(){ return post_T; }
friend istream &operator>>(istream &in, Teacher &ob);
friend ostream &operator<<(ostream &out, Teacher &ob);
};
//Teacher类
istream &operator>>(istream &in, Teacher &ob)
{
in >> ob.system >> ob.post_T >> ob.major;
return in;
}
ostream &operator<<(ostream &out, Teacher &ob)
{
out << ob.system << ob.post_T << ob.major << endl;
return out;
}
class Teacher_Manage
{
Teacher Tea[max];
int top;//记录个数f
public:
Teacher_Manage()
{
top = 1;
}
void Add();//添加
void show();//显示
void search();//查找
void Edit(){}//修改
void Delete();//删除
void Total();//统计
void save();//保存
void read();//读取
};
void Number::show()
{
ifstream in("f.txt", ios::in);
if (!in)
{
cout << "can't open it!" << endl;
return;
}
cout << "基本信息:" << endl;
cout << "姓名" << name << "学号" << num << "性别" << sex << "年龄" << age << endl;
in.close();
}
void Number::input()
{
ofstream out("f.txt", ios::out);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
cout << "录入基本信息:" << endl;
cout << "姓名" << endl;
cin >> name;
cout << "学号" << endl;
cin >> num;
cout << "性别" << endl;
cin >> sex;
cout << "年龄" << endl;
cin >> age;
out.close();
}
void Teacher::show()
{
ifstream in("f.txt", ios::in);
if (!in)
{
cout << "can't open it!" << endl;
return;
}
Number::show();
cout << "系部" << system << "专业" << major << "职位" << post_T << endl;
in.close();
}
void Teacher::input()
{
ofstream out("f.txt",ios::out);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
Number::input();
cout << "系部" << endl;
cin >> system;
cout << "专业" << endl;
cin >> major;
cout << "职位" << endl;
cin >> post_T;
out.close();
}
void Teacher_Manage::Add()
{
void Teacher_Menu();
ofstream out("f.txt",ios::out);
ifstream in("f.txt",ios::in);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
if (top >= max)
{
cout << "人员已满" << endl;
return;
}
cout << "1,输入,0,结束输入" << endl;
int a = 1;
while (a)
{
//fflush(stdin);
if (a == 1)
{
cout << "请输入你需要添加的人员信息:" << endl;
Teacher t;
t.input();
for (int i = 0; i < top; i++)
{
if (t.Get_num() == Tea[i].num)
{
cout << "该编号已存在!请重新输入:" << endl;
continue;
}
else
{
Tea[i] = t;
top=top+1;
out << top;
}
out << Tea[i].name<<Tea[i].num<<Tea[i].sex<<Tea[i].age <<t<< endl;
}
//fflush(stdin);
out.close();
//cout << "y是否继续输入,n是退出输入" << endl;
//int b = getchar();
}
cin >> a;
if (a == 0)
{
//break;
Teacher_Menu();
}
}
}
//教师的添加功能
void Teacher_Manage::search()
{
ifstream in("f.txt",ios::in);
ofstream out("f.txt",ios::out);
in >> top;
if (!in)
{
cout << "can't open it!" << endl;
return;
}
if (top == 0)
{
cout << "无人员记录!" << endl;
return;
}
Teacher_Manage tm;
Teacher t;
void Teacher_Menu();
int j = 1;
while (j)
{
if (j == 1)
{
cout << "输入查询方式:1.按编号查找\t2.按姓名查找" << endl;
int n;
cin >> n;
switch (n)
{
case 1:{
cout << "请输入要查找的编号:\n" << endl;
string num;
cin >> num;
for (int i = 0; i < top; i++)
{
if (num == Tea[i].num)
{
cout << "该人员信息如下:" << endl;
in >> Tea[i];
tm.show();
}
}
}
break;
case 2:{
cout << "请输入要查找的姓名:" << endl;
string name;
cin >> name;
for (int i = 0; i < top; i++)
{
if (name == Tea[i].name)
{
cout << "该人员信息如下:" << endl;
in >> Tea[i];
//in >> Tea[i].name >> Tea[i].num >> Tea[i].sex >> Tea[i].age >> t;
tm.show();
}
}
}
break;
default:cout << "查无此人!" << endl; break;
}
}
cin >> j;
}
Teacher_Menu();
in.close();
}
//教师人员查询
void Teacher_Manage::show()
{
void Teacher_Menu();
Teacher t;
ifstream in("f.txt",ios::in);
if (!in)
{
cout << "can't open it!" << endl;
return;
}
if (top == 0)
{
cout << "无数据!" << endl;
return;
}
in >> top;
for (int i = 0; i < top; i++)
{
cout << "第i个人员的信息:" << i << endl;
in >> Tea[i];
//in >> Tea[i].name>>Tea[i].num>>Tea[i].sex>>Tea[i].age>>t;
//cout << Tea[i].name << Tea[i].num << Tea[i].sex << Tea[i].age << t;
Tea[i].show();
}
Teacher_Menu();
in.close();
}
//教师显示
void Teacher_Manage::Delete()
{
void Teacher_Menu();
ofstream out("f.txt",ios::out);
ifstream in("f.txt", ios::in);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
if (top == 0)
{
cout << "无数据可操作" << endl;
return;
}
cout << "选择删除方法:" << endl;
cout << "1.编号" << "2.姓名" << endl;
switch (int x = getchar())
{
case 1:
{cout << "请输入要删除的编号" << endl;
string num1;
cin >> num1;
for (int i = 0; i < top; i++)
{
in >> Tea[i].num;
if (num1 == Tea[i].num)
{
for (int j = i; j < top - 1; j++)
{
Tea[j] = Tea[j + 1];
top--;
out << Tea[j] << top << endl;
}
}
else
{
cout << "无此人信息" << endl;
}
}
}
break;
case 2:
{
cout << "请输入要删除的姓名:" << endl;
string name;
cin >> name;
for (int i = 0; i < top; i++)
{
in >> name;
if (name == Tea[i].name)
{
for (int j = i; j < top - 1; j++)
{
Tea[j] = Tea[j + 1];
top--;
out << Tea[j] << top << endl;
}
}
else
{
cout << "无此人信息" << endl;
}
}
}
break;
default:break;
}
Teacher_Menu();
out.close();
in.close();
}
//Teacher人员信息删除操作
void Teacher_Manage::Total()
{
void Teacher_Menu();
ofstream out("f.txt",ios::out);
ifstream in("f.txt",ios::in);
if (!out)
{
cout << "can't open it!" << endl;
return;
}
cout << "1.按人数统计 2.按男女统计" << endl;
int a = 0, b = 0;
in >> top;
cin >> a;
switch (a)
{
case 1: cout << "共有" << top << "个" << endl; break;
case 2:{
string s = "男";
string n = "女";
for (int i = 0; i < top; i++)
{
in >> Tea[i].sex;
if (s == Tea[i].sex || n == Tea[i].sex)
{
a++;
b++;
out << a << b;
}
}
cout << "男生有" << a << "个" << "\n" << "女生有" << b << "个" << endl;
}
}
Teacher_Menu();
out.close();
in.close();
}
//Teacher人员信息统计操作
void Teacher_Manage::save()
{
Teacher t;
void Teacher_Menu();
ofstream out("f.txt", ios::out);
ifstream in("f.txt", ios::in);
if (!out)
{
cout << "can not!" << endl;
return;
}
in >> top;
for (int i = 0; i < top; i++)
{
while (cin >> Tea[i].name >> Tea[i].num >> Tea[i].sex >> Tea[i].age >> t)
{
out << Tea[i].name << Tea[i].num << Tea[i].sex << Tea[i].age << t << endl;
}
}
Teacher_Menu();
out.close();
}
void Teacher_Manage::read()
{
ifstream in("f.txt", ios::in);
void Teacher_Menu();
Teacher t;
if (!in)
{
cout << "打开失败!" << endl;
return;
}
int i = 0;
in >> top;
for (; i < top; i++)
{
while (in >> Tea[i].name >> Tea[i].num>> Tea[i].sex >> Tea[i].age >> t)
{
//Tea[i].show();
cout << Tea[i].name << Tea[i].num << Tea[i].sex << Tea[i].age << t << endl;
i++;
top++;
}
}
Teacher_Menu();
in.close();
}
//
void Teacher_Menu()
{
Teacher_Manage T_M;
void firstmenu();
cout << "请输入你的选择:" << endl;
int n;
cout << "\t\t" << "1.添加" << "\t" << "\t" << endl;
cout << "\t\t" << "2.显示" << "\t" << "\t" << endl;
cout << "\t\t" << "3.查询" << "\t" << "\t" << endl;
cout << "\t\t" << "4.编辑" << "\t" << "\t" << endl;
cout << "\t\t" << "5.删除" << "\t" << "\t" << endl;
cout << "\t\t" << "6.统计" << "\t" << "\t" << endl;
cout << "\t\t" << "7.保存" << "\t" << "\t" << endl;
cout << "\t\t" << "8.读取" << "\t" << "\t" << endl;
cout << "\t\t" << "9.black" << "\t" << "\t" << endl;
cin >> n;
switch (n)
{
case 1:T_M.Add(); break;
case 2:T_M.show(); break;
case 3:T_M.search(); break;
case 4:T_M.Edit(); break;
case 5:T_M.Delete(); break;
case 6:T_M.Total(); break;
case 7:T_M.save(); break;
case 8:T_M.read(); break;
//case 9:firstmenu(); break;
default:
break;
}
//cout << "结束请按0!" << endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
Teacher_Menu();
system("pause");
return 0;
}