📄 信息修改.cpp
字号:
#include<iostream>
#include<fstream>
#include<iomanip>
#include"学生信息定义.h"
using namespace std;
void student::xiugai()
{
int i,j,b,l;
char c;
string a;
student stu[60];
// string name;
ifstream infile("studentnum.txt",ios::in);
if(!infile)
{
cerr<<"studentnum.txt open error:"<<endl;
exit(1);
}
infile>>stu_num;
infile.close();
ifstream infile1("student.txt",ios::in);
if(!infile1)
{
cerr<<"student.txt open error:"<<endl;
exit(1);
}
l2: cout<<"请输入您要修改信息的学生姓名:"<<endl;
cin>>name;
for(i=1;i<=stu_num;i++)
{
infile1>>stu[i].name
>>stu[i].number
>>stu[i].age
>>stu[i].sex
>>stu[i].addr
>>stu[i].phone
>>stu[i].room_num;
}infile1.close();
for(i=1;i<=stu_num;i++)
{
if(name==stu[i].name)
{
cout<<"您要修改的学生信息为:"<<endl;
cout<<""<<endl;
cout<<"姓名 学号 年龄 性别 家庭住址 电话 寝室号"<<endl;
cout<<setw(2)<<stu[i].name
<<setw(7)<<stu[i].number
<<setw(6)<<stu[i].age
<<setw(6)<<stu[i].sex
<<setw(16)<<stu[i].addr
<<setw(17)<<stu[i].phone
<<setw(8)<<stu[i].room_num<<endl;
l=i;
// }
// }
cout<<""<<endl;
cout<<"************************************************"<<endl;
cout<<"1.姓名2.学号3.年龄4.性别5.家庭住址6.电话7.寝室号"<<endl;
cout<<"************************************************"<<endl;
cout<<""<<endl;
cout<<""<<endl;
l1: cout<<"请选择您要修改内容的编号!"<<endl;
cin>>j;
cout<<""<<endl;
switch(j)
{
case 1:cout<<"请输入您要修改的内容!"<<endl;cin>>a;stu[l].name=a;break;
case 2:cout<<"请输入您要修改的内容!"<<endl;cin>>a;stu[l].number=b;break;
case 3:cout<<"请输入您要修改的内容!"<<endl;cin>>a;stu[l].age=b;break;
case 4:cout<<"请输入您要修改的内容!"<<endl;cin>>a;stu[l].sex=c;break;
case 5:cout<<"请输入您要修改的内容!"<<endl;cin>>a;stu[l].addr=a;break;
case 6:cout<<"请输入您要修改的内容!"<<endl;cin>>a;stu[l].phone=a;break;
case 7:cout<<"请输入您要修改的内容!"<<endl;cin>>a;stu[l].room_num=b;break;
default:cout<<"你输入的数字有误,请重新输入。"<<endl;goto l1;//break;
}
cout<<""<<endl;
}
// if(name!=stu[i].name)
// {
// cout<<"无此人"<<endl;goto l2;
// }
}
// cout<<"修改后的内容变为:"<<endl;
ofstream outfile2("student.txt",ios::out);
if(!outfile2)
{
cerr<<"student.txt open error:"<<endl;
exit(1);
}
for(i=1;i<=stu_num;i++)
{
outfile2<<stu[i].name
<<" "<<stu[i].number
<<" "<<stu[i].age
<<" "<<stu[i].sex
<<" "<<stu[i].addr
<<" "<<stu[i].phone
<<" "<<stu[i].room_num<<endl;
}
outfile2.close();
cout<<"信息修改成功!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
}
/*outfile2.close();
ifstream infile3("student.txt",ios::in);
cout<<"姓名 学号 年龄 性别 家庭住址 电话 寝室号"<<endl;
for(i=0;i<=stu_num;i++)
{
infile3>>stu[i].name
>>stu[i].number
>>stu[i].age
>>stu[i].sex
>>stu[i].addr
>>stu[i].phone
>>stu[i].room_num;
if(name==stu[i].name)
cout<<setw(2)<<stu[i].name
<<setw(6)<<stu[i].number
<<setw(5)<<stu[i].age
<<setw(5)<<stu[i].sex
<<setw(14)<<stu[i].addr
<<setw(15)<<stu[i].phone
<<setw(7)<<stu[i].room_num<<endl;
}infile3.close();
}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -