C++ 输出错误(新增问题)

C++ Output Errors (New Issues added)

我的程序似乎只 运行 完成 2 门课程,然后它停止并显示第二门课程 3 次。

我被难住了,拔头发,任何建议都会有很大帮助。

我也提前为冗长道歉post,但我觉得需要所有部分来确定问题。

这是当前的输出: Current output

我的预期输出是:

标题:标题课程 ID:12345 Session:54321 学分:1.2

这里是错误所在:

    #include <fstream>
    #include <string>
    #include <iomanip>

    #include "Course.h"

    using namespace std;

    int main()
    {
    string title;
    double units;
    int course;
    int session;

    cout << "Enter the title of a course:";
    cin >> title;
    cout << "Enter the course ID number";
    cin >> course;
    cout << "Enter the number of units for the course";
    cin >> units;
    cout << "Enter the session number for the course";
    cin >> session;

    Courses Class_1(title, session, units, course);

    cout << endl << "Class title: " << Class_1.getTitle() << endl <<
        "title: " << setw(5) << Class_1.getTitle() <<
        setw(5) << "course ID: " << setw(5) << Class_1.getCourseID() <<
        setw(5) << "Session number: " << setw(5) << Class_1.getSessionNumber()       <<
        setw(5) << "Units: " << setw(5) << Class_1.getNumOfUnits() << endl;

    cout << "Enter the title of a course:";
    cin >> title;
    cout << "Enter the course ID number";
    cin >> course;
    cout << "Enter the number of units for the course";
    cin >> units;
    cout << "Enter the session number for the course";
    cin >> session;

    Courses Class_2;

    Class_2.setTitle(title);
    Class_2.setCourseID(course);
    Class_2.setSessionNumber(session);
    Class_2.setNumOfUnits(units);

    cout << endl << "Class title: " << setw(5) << Class_2.getTitle() << endl <<
        "title: " << setw(5) << Class_2.getTitle() <<
        setw(5) << "course ID: " << setw(5) << Class_2.getCourseID() <<
        setw(5) << "Session number: " << setw(5) << Class_2.getSessionNumber() <<
        setw(5) << "Units: " << setw(5) << Class_2.getNumOfUnits() << endl;

    cout << Class_1.getTitle() << endl;
    // -----------------------------------------------------------------

    Courses Class_3;

    Class_3.setTitle(title);
    Class_3.setCourseID(course);
    Class_3.setSessionNumber(session);
    Class_3.setNumOfUnits(units);

    cout << endl << "Class title: " << setw(5) << Class_3.getTitle() << endl <<
        "title: " << setw(5) << Class_3.getTitle() <<
        setw(5) << "course ID: " << setw(5) << setw(5) << Class_3.getCourseID() <<
        setw(5) << "Session number: " << setw(5) << setw(5) << Class_3.getSessionNumber() <<
        setw(5) << "Units: " << setw(5) << Class_3.getNumOfUnits() << endl;

    cout << Class_2.getTitle() << endl;
    // -----------------------------------------------------------------------

    Courses Class_4;

    Class_4.setTitle(title);
    Class_4.setCourseID(course);
    Class_4.setSessionNumber(session);
    Class_4.setNumOfUnits(units);

    cout << endl << "Class title: " << setw(5) << Class_4.getTitle() << endl <<
        "title: " << setw(5) << Class_4.getTitle() <<
        setw(5) << "course ID: " << setw(5) << Class_4.getCourseID() <<
        setw(5) << "Session number: " << setw(5) << Class_4.getSessionNumber() <<
        setw(5) << "Units: " << setw(5) << Class_4.getNumOfUnits() << endl;

    cout << Class_3.getTitle() << endl;

    system("pause");
    return 0;
    }

这是header:

    #pragma once
    #include <iostream>
    #include <string>
    using namespace std;

    #ifndef COURSES_H
    #define COURSES_H

    class Courses
    {
    public:
    // Default Constructor
    Courses();

    // Ovverload Constructor
    Courses(string title, int course, int session, double units);

    // Destructor
    ~Courses();

    int getCourseID() const;
    // Gets the course ID.

    int getSessionNumber() const;

    // Gets the session number.

    double getNumOfUnits() const;
    // Gets the number of units.

    string getTitle() const;
    // Gets the title of a course.

    // --------------------------------

    void setCourseID(int);
    // Sets the ID number of a course.

    void setSessionNumber(int);
    // Sets the session number of a course.

    void setNumOfUnits(double);
    // Sets the number of units of a course.

    void setTitle(string);
    // Sets the title of a course.



    private:

    // Member Variables
    string newTitle;
    int newCourseID;
    int newSessionNumber;
    double newNumOfUnits;

    };

    #endif // !COURSE_H

这里是Course.cpp

    #include "Course.h"

    Courses::Courses()
    {
    newCourseID = 0;
    newSessionNumber = 0;
    newNumOfUnits = 0.0;
    }



    Courses::Courses(string title, int course, int session, double units)
    {
    newTitle = title;
    newCourseID = course;
    newSessionNumber = session;
    newNumOfUnits = units;
    }

    Courses::~Courses()
    {
    }

    string Courses::getTitle() const
    {
    return newTitle;
    }

    int Courses::getCourseID() const
    {
    return newCourseID;
    }

    double Courses::getNumOfUnits() const
    {
    return newNumOfUnits;
    }

    int Courses::getSessionNumber() const
    {
    return newSessionNumber;
    }
    // -------------------------------------
    void Courses::setTitle(string title)
    {
    newTitle = title;
    }

    void Courses::setCourseID(int course)
    {
    newCourseID = course;
    }

    void Courses::setNumOfUnits(double units)
    {
    newNumOfUnits = units;
    }

    void Courses::setSessionNumber(int session)
    {
    newSessionNumber = session;
    }

您的构造函数声明为 Courses(string title, int course, int session, double units)

但你称它为 Courses Class_1(title, session, units, course);

只要切换一下变量就可以了

Courses Class_1(title,course,session,units);