gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:39:42 25 4
gpt4 key购买 nike

我的程序似乎只运行了 2 门类(class),然后它停止并显示了 3 次第二门类(class)。

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

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

这是当前的输出: Current output

我的预期输出是:

标题:标题类(class) ID:12345 课时: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;
}

这是标题:

    #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);

关于C++ 输出错误(新增问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37405800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com