遗失的世界779 2023-04-16 11:24 采纳率: 83.3%
浏览 77
已结题

读取字符串字符时出错

创建的数组数据正常,但我给对象实例化的时候,数据传入就会出现读取字符串时出错,请问是什么原因?
代码功能:输入三组学生的姓名和成绩,进行求综合成绩,然后排序,再输出成绩
代码如下:

#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
    string studentName;
    float englishGrade;
    float mathGrade;
    float politicGrade;
    float majorGrade;
public:
    void setStudentAllInformation(string studentName, float englishGrade, float mathGrade, float politicGrade, float majorGrade)
    {
        this->studentName = studentName;
        this->englishGrade = englishGrade;
        this->mathGrade = mathGrade;
        this->politicGrade = politicGrade;
        this->majorGrade = majorGrade;
    }
    string getStudentName()
    {
        return studentName;
    }
    void getScore()
    {
        cout << "英语:" << this->englishGrade;
        cout << "数学:" << this->mathGrade;
        cout << "政治:" << this->politicGrade;
        cout << "专业课:" << this->majorGrade;

    }
    double getTotalGrade()
    {
        return (this->englishGrade * 0.15 + this->mathGrade * 0.2 + this->politicGrade * 0.2 + this->majorGrade * 0.45);
        /*cout<<"英语成绩为:";cin>>englishGrade;
        cout<<"数学成绩为:";cin>>mathGrade;
        cout<<"政治成绩为:";cin>>politicGrade;
        cout<<"专业课成绩为:";cin>>majorGrade;*/
    }
};
class SortSystem
{
private:
    Student _s[3];
public:

    SortSystem(Student* _s)
    {
        _s[3] = *_s;
    }
    void Sort()
    {
        for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 2 - i; i++)
            {
                if (_s[j].getTotalGrade() < _s[j + 1].getTotalGrade())
                {
                    Student a;
                    a = _s[j];
                    _s[j] = _s[j + 1];
                    _s[j + 1] = a;
                }
            }

        }
    }
    void ShowAllStudentsScores()
    {
        for (int i = 0; i < 3; i++)
        {
            cout << _s[i].getStudentName() << "的各科成绩为:" << endl;
            _s[i].getScore();
            cout << "综合成绩:" << _s[i].getTotalGrade() << endl;
        }
    }
};
int main(void)
{
    string _studentName;
    float _englishGrade, _mathGrade, _politicGrade, _majorGrade;
    Student students[3];
    for (int i = 0; i < 3; i++)
    {
        cin >> _studentName >> _englishGrade >> _mathGrade >> _politicGrade >> _majorGrade;
        students[i].setStudentAllInformation(_studentName, _englishGrade, _mathGrade, _politicGrade, _majorGrade);
    }
    SortSystem RJ2202(students);
    RJ2202.ShowAllStudentsScores();
    RJ2202.Sort();

    return 0;
}

img

img

  • 写回答

2条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-04-16 11:44
    关注

    排序对象,数组初始化有问题
    修改如下:

    #include<iostream>
    #include<string>
    using namespace std;
    class Student
    {
    private:
        string studentName;
        float englishGrade;
        float mathGrade;
        float politicGrade;
        float majorGrade;
    public:
        void setStudentAllInformation(string studentName, float englishGrade, float mathGrade, float politicGrade, float majorGrade)
        {
            this->studentName = studentName;
            this->englishGrade = englishGrade;
            this->mathGrade = mathGrade;
            this->politicGrade = politicGrade;
            this->majorGrade = majorGrade;
        }
        string getStudentName()
        {
            return studentName;
        }
        void getScore()
        {
            cout << "英语:" << this->englishGrade;
            cout << "数学:" << this->mathGrade;
            cout << "政治:" << this->politicGrade;
            cout << "专业课:" << this->majorGrade;
     
        }
        double getTotalGrade()
        {
            return (this->englishGrade * 0.15 + this->mathGrade * 0.2 + this->politicGrade * 0.2 + this->majorGrade * 0.45);
            /*cout<<"英语成绩为:";cin>>englishGrade;
            cout<<"数学成绩为:";cin>>mathGrade;
            cout<<"政治成绩为:";cin>>politicGrade;
            cout<<"专业课成绩为:";cin>>majorGrade;*/
        }
    };
    class SortSystem
    {
    private:
        Student *_s; // 修改
    public:
     
        SortSystem(Student* _s)
        {
            this->_s = _s; // 修改
        }
        void Sort()
        {
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2 - i; i++)
                {
                    if (_s[j].getTotalGrade() < _s[j + 1].getTotalGrade())
                    {
                        Student a;
                        a = _s[j];
                        _s[j] = _s[j + 1];
                        _s[j + 1] = a;
                    }
                }
     
            }
        }
        void ShowAllStudentsScores()
        {
            for (int i = 0; i < 3; i++)
            {
                cout << _s[i].getStudentName() << "的各科成绩为:" << endl;
                _s[i].getScore();
                cout << "综合成绩:" << _s[i].getTotalGrade() << endl;
            }
        }
    };
    int main(void)
    {
        string _studentName;
        float _englishGrade, _mathGrade, _politicGrade, _majorGrade;
        Student students[3];
        for (int i = 0; i < 3; i++)
        {
            cin >> _studentName >> _englishGrade >> _mathGrade >> _politicGrade >> _majorGrade;
            students[i].setStudentAllInformation(_studentName, _englishGrade, _mathGrade, _politicGrade, _majorGrade);
        }
        SortSystem RJ2202(students);
        RJ2202.ShowAllStudentsScores();
        RJ2202.Sort();
     
        return 0;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 4月17日
  • 已采纳回答 4月17日
  • 创建了问题 4月16日

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面