将客 2024-06-16 22:39 采纳率: 96.6%
浏览 0
已结题

为什么选择功能会是这样子


#include<fstream>
#include<iostream>
#include<string>
#include<cstdlib>
#include<vector>
#include<algorithm>
using namespace std;
//定义时间日期类表示学生的出生年月日
class Date
{
public:
void SetDate () //设置年月
{
cout<<"请输入出生年份:";
cin>>year;
cout<<"请输入出生月份:";
cin>>month;
cout<<"请输入出生日:";
cin>>day;
}
int GetYear()
 {return year;}//获取年份
int GetMonth() 
{return month;}//获取月
int GetDay()
 {return day;} //获取日
void SetBirthDate()
{SetDate();}
private:
int year,month,day;
};
//定义学生类,包含学生的学号、姓名、性别、出生年月日
class Student
{
public:
void SetStuValues (Date &birthday)
{
cout<<"请输入学生的学号:";
cin>>num;
cout<<"请输入学生的姓名:";
cin>>name;
cout<<"请输入学生的性别(M/F)或(f/m):";
cin>>gender;
birthday.SetDate ();
cout<<"请输入学生3门课成绩:";
cin>>score1>>score2>>score3;
average=(score1+score2+score3)/3.0;
minscore=min({score1,score2,score3});
maxscore=max({score1,score2,score3});
}
void SetBirthday (Date &birthday) //设置年月日
{
 birthday.SetDate();
}
int GetBirYear (Date &birthday) //获得年
{return birthday.GetYear();
}
int GetBirMonth (Date &birthday) //获得月
{
  return birthday.GetMonth();
}
int GetBirDay (Date &birthday) //获得日
{
return birthday.GetDay();    
}
void InputStudent (Date &birthday)
{
char choice;
cout<<"输入学生记录:"<<endl;
ofstream outfile ("D:\\DEV\\abc.txt", ios::app);//打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
SetStuValues (birthday);
outfile <<num<<" "<<name<<" "<<birthday.GetYear() <<" "<<birthday.GetMonth ()<<" "<<birthday.GetDay ()
<<" "<<gender<<" "<<score1<<" "<<score2<<" "<<score3<<"  "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
cout<<"是否继续输入学生信息输入y(继续)或者n(停止):";
cin>>choice;
if(choice=='y')
{
InputStudent (birthday);
}
outfile.close ();
system ("pause");
}
//2.浏览全部学生记录
void OutputStudent ()
{
int year;
int month;
int day;
char gender;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
while (infile>>num>>name>>year>>month>>day>>gender>>score1>>score2>>score3>>average>>maxscore>>minscore)
{
cout<<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<"  "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
infile.close ();
system ("pause");
}
//3. 按学号查找学生记录
void FindByNum ()
{
int year;
int month;
int day;
char gender;
int findnum;
bool find = false;
cout<<"请输入要查找学生的学号:";
cin>>findnum;
ifstream infile ("D:\\DEV\\abc.txt" , ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day>>score1>>score2>>score3>>gender>>average>>maxscore>>minscore;
if (num ==findnum)
{
cout<<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<"  "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
find = true;
}
infile.get ( );
if (infile.peek ()=='\n') break;
}
if(! find)
{cout<<"查无此人"<<endl;
}
infile.close ();
system ("pause");
}
//4. 按学号删除学生记录
void DeleteByNum ()
{
int year;
int month;
int day;
char gender;
int findnum;
bool find = false;
cout<<"请输入要删除学生的学号:";    
cin>>findnum;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt", ios::app);
if(! outfile) //打开文件
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day>>score1>>score2>>score3>>gender>>average>>maxscore>>minscore;
if (num != findnum)
{
outfile <<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<"  "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{
cout<<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<"  "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
find = true;
cout<<"已经删除该学生"<<endl;
}
infile.get ();
if (infile.peek ()=='\n') break;
}
if (! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove ("D:\\DEV\\abc.txt" ) ; //删除文件
rename ("temp.txt" , "D:\\DEV\\abc.txt" ) ;
}
//5.按学号修改学生的出生日期
void AlterNameByNum ()
{
int year;
int month;
int day; 
int findnum;
int alteryear;
int altermonth;
int alterday;
bool find = false;
cout<<"请输入要修改学生的学号:";
cin>>findnum;
cout<<"请输入要修改学生的出生日期:";
cin>>alteryear>>altermonth>>alterday;
ifstream infile ("D:\\DEV\\abc.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>year>>month>>day>>gender>>score1>>score2>>score3>>average>>maxscore>>minscore;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<year<<" "<<month<<" "<<day<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<"  "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<gender<<" "<<
score1<<" "<<score2<<" "<<score3<<"  "<<average<<" "<<maxscore<<" "<<minscore<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" D:\\DEV\\abc.txt" ) ; //删除文件
rename ("temp.txt", "D:\\DEV\\abc.txt");
}
//6.按学号修改学生的出生日期
void AlterScoreByNum ()
{
int findnum;
int alteryear;
int altermonth;
int alterday;
int newScore1;
int newScore2;
int newScore3;
bool find = false;
cout<<"请输入要修改学生的学号:";
cin>>findnum;
cout<<"请输入要修改学生的成绩:";
cin>>newScore1>>newScore2>>newScore3;
newaverage=(newScore1+newScore2+newScore3)/3.0;
newmin=min({newScore1,newScore2,newScore3});
newmax=max({newScore1,newScore2,newScore3});
fstream file ("D:\\DEV\\abc.txt");
if(!file)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("temp.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (!file.eof ())
{
file>>num>>name>>alteryear>>altermonth>>alterday>>gender>>score1>>score2>>score3>>newaverage>>newmax>>newmin;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<newaverage<<" "<<newmax<<" "<<newmin<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<gender<<" "<<
newScore1<<" "<<newScore2<<" "<<newScore3<<"  "<<newaverage<<" "<<newmax<<" "<<newmin<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
file.get ();
if (file.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
file.close ();
outfile.close ();
remove ("D:\\DEV\\abc.txt");
rename ("temp.txt", "D:\\DEV\\abc.txt");
}
private:
int num;
string  name;
Date birthday; 
char gender;
int newmax;
int newmin;
int minscore;
int maxscore;
double average;
double newaverage;
int score1,score2,score3;
};
void MainShow ()
{
cout<<"\t学生信息系统"<<endl;
cout<<"\t1.输入学生记录"<<endl;
cout<<"\t2.浏览全部学生记录"<<endl;
cout<<"\t3.按学号查找学生记录"<<endl;
cout<<"\t4.按学号删除学生记录"<<endl;
cout<<"\t5.按学号修改学生的出生日期"<<endl;
cout<<"\t6.按学号修改学生的成绩"<<endl;
cout<<"\t7.退出系统"<<endl;
}
void Select()
{
int num;
Student stu;
Date birthday;
cout<<"请选择功能:";
cin>>num;
switch(num)
{
case 1:
stu.InputStudent (birthday);
Select ();
break;
case 2:
stu.OutputStudent ();
Select ();
break;
case 3:
stu.FindByNum ();
Select ();
break;
case 4: 
stu.DeleteByNum ();
Select ();
break;
case 5:
stu.AlterNameByNum();
Select ();
break;
case 6:
stu.AlterScoreByNum();
Select ();
break;
case 7:
cout<<"系统结束,谢谢再见"<<endl;
exit (0);
break;
}
}
int main ()
{
MainShow ();
Select ();
return 0;
}

为什么选择功能会是这样子,没有对功能4做出反应

  1. img


    并且temple文件里面的内容是这样子的

    img

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-06-16 22:39
    关注

    引自免费微信小程序:皆我百晓生

    从你提供的代码来看,问题主要出现在DeleteByNum()函数中的数据读取和处理上。在尝试删除具有特定学号的学生记录时,程序未能正确地读取和比较数据,导致了异常行为。具体来说,问题可能源于以下几个方面:

    1. 错误的数据类型匹配:在读取文件时,你使用了>>操作符来读取数据到变量中。但是,你的数据文件中包含了空格分隔的值,而>>操作符在遇到空白字符时会停止读取。然而,在某些字段如genderaverage等处,你可能读入了非预期的数据类型,因为这些字段的数据类型与实际读取的数据格式不匹配。

    2. EOF判断错误! infile.eof()在循环条件中使用是不正确的,因为eof()通常在流尝试读取但未成功读取任何数据后返回true。因此,它不应在读取操作之前被检查。更好的做法是在读取操作之后检查是否到达文件末尾。

    3. 额外的输入操作infile.get()if (infile.peek()=='\n') break;这样的代码可能会导致读取混乱,尤其是在循环中。这可能是因为它们改变了文件指针的位置,影响了后续的读取操作。

    为了解决上述问题,可以对DeleteByNum()函数进行以下修改:

    • 确保所有字段的数据类型与文件中的数据格式相匹配。
    • 使用更安全的方法来检测文件的末尾,比如检查读取操作是否成功。
    • 移除不必要的infile.get()if (infile.peek()=='\n') break;代码行,因为它们可能导致读取位置错误。

    下面是一个修改后的DeleteByNum()函数示例:

    void DeleteByNum ()
    {
        int findnum;
        cout<<"请输入要删除学生的学号:";
        cin>>findnum;
    
        ifstream infile ("D:\\DEV\\abc.txt", ios::in);
        if(! infile)
        {
            cout<<"文件打开失败"<<endl;
            return;
        }
    
        ofstream outfile ("temp.txt", ios::app);
        if(! outfile)
        {
            cout<<"文件打开失败"<<endl;
            infile.close();
            return;
        }
    
        bool find = false;
        string line;
        while (getline(infile, line)) // 使用getline读取整行,避免类型不匹配问题
        {
            stringstream ss(line); // 将整行转换为stringstream,便于读取
            int num, year, month, day, score1, score2, score3, minscore, maxscore;
            double average;
            string name, gender;
            
            if (!(ss >> num >> name >> year >> month >> day >> gender >> score1 >> score2 >> score3 >> average >> maxscore >> minscore))
            {
                // 如果读取失败,可能是数据格式错误,跳过这一行
                continue;
            }
            
            if (num != findnum)
            {
                outfile << line << endl; // 复制不是目标学号的行到临时文件
            }
            else
            {
                find = true;
                cout<<"已经删除该学生"<<endl;
            }
        }
    
        if (! find)
        {
            cout<<"查无此人"<<endl;
        }
    
        infile.close();
        outfile.close();
    
        remove ("D:\\DEV\\abc.txt" ); // 删除原文件
        rename ("temp.txt", "D:\\DEV\\abc.txt"); // 重命名临时文件为原文件名
    }
    

    这个修改版使用了getline来读取整个行,然后通过stringstream来解析每一行的数据,确保数据类型匹配,并且避免了直接使用>>操作符可能带来的问题。同时,去掉了不必要的infile.get()if (infile.peek()=='\n') break;语句,使代码更加清晰和稳定。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月25日
  • 已采纳回答 6月17日
  • 创建了问题 6月16日

悬赏问题

  • ¥30 模拟电路 logisim
  • ¥15 PVE8.2.7无法成功使用a5000的vGPU,什么原因
  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价