将客 2024-06-15 22:01 采纳率: 96.6%
浏览 1
已结题

关于#c++#的问题:输入按照这样子生成的两个文本文件copy文件里面的内容为什么是这样的这是student文件里面的内容为什么实现不了程序的功能呢

#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):";
cin>>gender;
birthday.SetDate ();
cout<<"请输入学生的第一门课成绩:";
cin>>score1;
cout<<"请输入学生的第二门课成绩:";
cin>>score2;
cout<<"请输入学生的第三门课成绩:";
cin>>score3;
average=(score1+score2+score3)/3.0;
minscore=min({score1,score2,score3});
maxscore=max({score1,score2,score3});
}
void SetNum () //设置学生的学号
{
cin>>num;
}
int GetNum () //获得学生的学号
{
return num;
}
void SetName ()//设置学生的姓
{
cin>>name;
}
string GetName ()//获得学生的姓名
{
return name;
}
void Setgender()//设置学生的性别
{
    cin>>gender;
}
char Getgender()//获得学生的性别
{
    return gender;
}
void Setscore1()
{
    cin>>score1;
}
int Getscore1()
{
    return score1;
}
void Setscore2()
{
    cin>>score2;
}
int Getscore2()
{
    return score2;
}
void Setscore3()
{
    cin>>score3;
}
int Getscore3()
{
return 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:\\student.txt", ios::app);//打开文件
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
SetStuValues (birthday);
outfile <<GetNum ()<<" "<<GetName()<<" "<<Getgender()<<" "
<<birthday.GetYear() <<" "<<birthday.GetMonth ()<<" "<<birthday.GetDay ()<<" "<<
Getscore1()<<" "<<Getscore2()<<" "<<Getscore3()<<" "<<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;
int score1,score2,score3;
double average;
ifstream infile ("d:\\student.txt", ios::in);
if (! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
infile. get ();
if (infile.peek ()=='\n') break;
}
infile.close ();
system ("pause");
}
//3. 按学号查找学生记录
void FindByNum ()
{
int year;
int month;
int day;
char gender;
int score1,score2,score3;
double average;
int findnum;
bool find = false;
cout<<"请输入要查找学生的学号:";
cin>>findnum;
ifstream infile ("d:\\student.txt" , ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num ==findnum)
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<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 score1,score2,score3;
int findnum;
bool find = false;
cout<<"请输入要删除学生的学号:";    
cin>>findnum;
ifstream infile ("d:\\student.txt", ios::in);
if(! infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("copy.txt", ios::app);
if(! outfile) //打开文件
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if (num != findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
}
else
{
cout<<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl; 
find = true;
cout<<"已经删除该学生"<<endl;
}
infile.get ();
if (infile.peek ()=='\n') break;
}
if (! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove ("d:\\student.txt" ) ; //删除文件
rename ("copy.txt" , " student.txt" ) ;
}
//5.按学号修改学生的出生日期
void AlterNameByNum ()
{
int count =0;
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:\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("copy.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<gender<<" "<<alteryear<<" "<<altermonth<<" "<<alterday<<" "<<score1<<" "<<
score2<<" "<<score3<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" d:\\student.txt" ) ; //删除文件
rename ("copy.txt", "student.txt");
}
//6.按学号修改学生的出生日期
void AlterScoreByNum ()
{
int count =0;
int year;
int month;
int day; 
int findnum;
int score11;
int score22;
int score33;
bool find = false;
cout<<"请输入要修改学生的学号";
cin>>findnum;
cout<<"请输入要修改学生的成绩";
cin>>score11>>score22>>score33;
ifstream infile ("d:\\student.txt", ios::in);
if(!infile)
{
cout<<"文件打开失败"<<endl;
}
ofstream outfile ("copy.txt" , ios::app) ;
if (! outfile)
{
cout<<"文件打开失败"<<endl;
exit (0);
}
while (! infile.eof ())
{
infile>>num>>name>>gender>>year>>month>>day>>score1>>score2>>score3;
if(num!=findnum)
{
outfile <<num<<" "<<name<<" "<<gender<<" "<<year<<" "<<month<<" "<<day<<" "<<
score1<<" "<<score2<<" "<<score3<<" "<<endl;
}
else
{ 
outfile <<num<<" "<<name<<" "<<gender<<" "<<score11<<" "<<score22<<" "<<score33<<" "<<endl;
 find = true;
cout<<"已经修改该学生"<<endl;
}
infile.get ();
if (infile.peek () =='\n') break;
}
if(! find)
{
cout<<"查无此人"<<endl;
}
infile.close ();
outfile.close ();
remove (" d:\\student.txt" ) ; //删除文件
rename ("copy.txt", "student.txt");
}
private:
int num;
string  name;
Date birthday; 
int minscore;
int maxscore;
double average;
char gender;
int score1,score2,score3;
};
void MainShow ()
{
cout<<"\t\t ***学生信息系统***"<<endl;
cout<<"\t\t ***1.输入学生记录**"<<endl;
cout<<"\t\t ***2.浏览全部学生记录***"<<endl;
cout<<"\t\t ***3.按学号查找学生记录**"<<endl;
cout<<"\t\t ***4.按学号删除学生记录***"<<endl;
cout<<"\t\t ***5.按学号修改学生的出生日期*** "<<endl;
cout<<"\t\t ***6.按学号修改学生的成绩*** "<<endl;
cout<<"\t\t ***7.退出系统***"<<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;
}

输入按照这样子

img


生成的两个文本文件

img


copy文件里面的内容为什么是这样的

img


这是student文件里面的内容

img


为什么实现不了程序的功能呢

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2024-06-15 22:01
    关注

    以下内容由CHATGPT及阿里嘎多学长共同生成、有用望采纳:


    根据您提供的代码和问题描述,我将尝试分析并提供一些可能的解决方案和建议。

    问题分析

    1. 文件路径问题

      • 您的代码中使用了硬编码的文件路径 "d:\\student.txt"。如果程序没有权限写入D盘,或者D盘不存在,这将导致文件打开失败。
    2. 文件操作逻辑

      • DeleteByNumAlterNameByNum函数中,您使用了removerename函数来删除原文件并重命名临时文件。如果原文件正在被使用(例如,您的程序正在运行并尝试访问该文件),这将导致操作失败。
    3. 用户输入验证

      • 您的代码中没有对用户输入进行验证。如果用户输入了非法的格式,程序可能会崩溃或行为异常。
    4. 变量混淆

      • AlterScoreByNum函数中,您使用了局部变量score11score22score33,但这些变量并没有被正确地赋值给学生对象的成员变量。
    5. 格式化输出问题

      • 从截图中可以看出,文件内容的格式与预期不符,可能是因为写入文件时的格式化字符串不正确。

    解决方案和建议

    1. 检查文件路径

      • 确保程序有权限写入指定的路径。如果需要,尝试使用相对路径或让用户选择文件路径。
    2. 改进文件操作逻辑

      • 在执行文件删除和重命名操作之前,确保文件不再被当前程序使用。可能需要在程序退出前关闭所有文件流。
    3. 增加用户输入验证

      • 在接受用户输入之前,添加验证逻辑以确保输入的合法性。
    4. 修正变量使用

      • AlterScoreByNum函数中,确保使用学生对象的成员变量来存储修改后的成绩。
    5. 检查格式化输出

      • 确保在写入文件时使用正确的格式化字符串,以匹配预期的文件内容格式。
    6. 使用异常处理

      • 考虑使用C++的异常处理机制来捕获并处理可能发生的文件操作错误。
    7. 调试和测试

      • 使用调试工具逐步执行程序,检查在每个步骤中变量的值和程序的行为是否符合预期。
    8. 参考资料

      • 您可以参考C++标准库中的文件流操作文档,了解如何正确使用ofstreamifstream
      • 学习如何使用C++的异常处理机制来提高程序的健壮性。

    代码示例修正

    以下是对AlterScoreByNum函数的一些修正建议:

    void AlterScoreByNum()
    {
        int findnum;
        int newScore1, newScore2, newScore3;
        cout << "请输入要修改学生的学号: ";
        cin >> findnum;
        cout << "请输入要修改学生的成绩: ";
        cin >> newScore1 >> newScore2 >> newScore3;
    
        ifstream infile("d:\\student.txt", ios::in);
        ofstream outfile("temp.txt", ios::out); // 使用临时文件
    
        if (!infile || !outfile)
        {
            cout << "文件打开失败" << endl;
            exit(0);
        }
    
        while (infile >> num >> name >> gender >> year >> month >> day >> score1 >> score2 >> score3)
        {
            if (num == findnum)
            {
                outfile << num << " " << name << " " << gender << " "
                        << year << " " << month << " " << day << " "
                        << newScore1 << " " << newScore2 << " " << newScore3 << endl;
                cout << "已经修改该学生的成绩" << endl;
            }
            else
            {
                outfile << num << " " << name << " " << gender << " "
                        << year << " " << month << " " << day << " "
                        << score1 << " " << score2 << " " << score3 << endl;
            }
        }
    
        infile.close();
        outfile.close();
        remove("d:\\student.txt"); // 删除原文件
        rename("temp.txt", "d:\\student.txt"); // 重命名临时文件
    }
    

    请注意,这只是一个示例,您可能需要根据您的具体需求进一步调整代码。希望这些建议能帮助您解决问题。如果您需要更多的帮助,例如对特定部分的代码进行调试,或者需要进一步的解释,请随时告诉我。

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

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 创建了问题 6月15日

悬赏问题

  • ¥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 数据包 大概什么价