将属性保存到类的C++程序问题



我对C++很陌生,一般来说没有太多编码经验。我很难弄清楚我的代码有什么问题,因为我所尝试的一切似乎都无法解决问题。在driver.cpp文件中,有一个addCourses函数,它使用student.cpp文件中的另一个addCourse函数。正如您在代码中看到的,numCourses应该在每次使用函数时递增(如果它满足if语句(。现在,在运行该函数时,numCourses确实会增加,但它不会保存给学生。例如,如果稍后在程序中我决定显示学生有多少门课程,它将显示0,这是默认值,而不是递增值。从长远来看,coursePrefix、courseNum和courseName也不会保存,student.cpp文件中的printSchedule函数只显示空白的默认构造函数值(我必须删除if语句,因为I=0永远不会小于numCourses,因为它一直默认为0(。请记住,这个程序是针对一个类的,有严格的限制,我不能真正使用任何高级代码。现在代码中的大部分内容都是我必须处理的概念。此外,我的老师说,我们可能不会为numCourses和schedule[]属性创建setter函数。

driver.cpp文件:

//Name: Driver
#include <iostream>
#include <fstream>
#include "student.h"
#define MAX 100
using namespace std;
int getStudents(int, Student[], string);
string findMaxStudent(int, Student[]);
void printStudentTable(int, Student[]);
int getMenuChoice();
int getStudentOption(int, Student[]);
void addCourses(Student);
enter code here
//comfirm error message for adding too many courses
//may need if statement in addCourses func to utilize true or false
int main(int argc, char const* argv[]){
int numStudents, userChoice, studentOption;
Student students[MAX];
string maxStudent;
if(argc != 2){
cout << "Correct usage:" << endl << argv[0] << " filename" << endl;
}
else{
string file = argv[1];
numStudents = getStudents(MAX, students, file);
if(numStudents >= 0){
maxStudent = findMaxStudent(numStudents, students);
do{
userChoice = getMenuChoice();
switch(userChoice){
case 0:
break;
case 1: printStudentTable(numStudents, students);
break;
case 2: studentOption = getStudentOption(numStudents, students);
addCourses(students[studentOption - 1]);
break;
case 3: studentOption = getStudentOption(numStudents, students);
students[studentOption - 1].printSchedule();
break;
case 4: cout << endl << "The student with the highest gpa is: " << maxStudent << endl;
break;
default: cout << "Please enter a valid option" << endl;
break;
}
}while(userChoice != 0);
}
}
return 0;
}
int getStudents(int maxSize, Student students[], string file){
int numStudents, number, i = 0;
string firstName, lastName;
float gpa;
ifstream fin(file.c_str());
if(fin){
while(fin && i < maxSize){
fin >> number >> lastName >> firstName >> gpa;
students[i].setNumber(number);
students[i].setLastName(lastName);
students[i].setFirstName(firstName);
students[i].setGpa(gpa);
i++;
}
}
else{
cout << "Sorry, could not open " << file << " for reading" << endl;
}
fin.close();
numStudents = i - 1;
return numStudents;
}
string findMaxStudent(int size, Student students[]){
string maxStudent;
float maxValue = 0;
for(int i = 0; i < size; i++){
if(students[i].getGpa() > maxValue){
maxValue = students[i].getGpa();
maxStudent = students[i].getFirstName() + " " + students[i].getLastName();
}
}
return maxStudent;
}
void printStudentTable(int numStudents, Student students[]){
cout.width(8); cout << left << "First";
cout.width(8); cout << left << "Last";
cout.width(6); cout << left << "Number" << endl;
cout << "======================" << endl;
for(int i = 0; i < numStudents; i++){
cout.width(8); cout << left << students[i].getFirstName();
cout.width(8); cout << left << students[i].getLastName();
cout.width(6); cout << left << students[i].getNumber() << endl;
}
}
int getMenuChoice(){
int menuChoice;
cout << endl;
cout << "STUDENT MANAGEMENT SYSTEM" << endl;
cout << "================================" << endl;
cout.width(8); cout << left << "1." << "Display Students" << endl;
cout.width(8); cout << left << "2." << "Add Courses" << endl;
cout.width(8); cout << left << "3." << "Display Student Schedule" << endl;
cout.width(8); cout << left << "4." << "Find Highest GPA Student" << endl;
cout.width(8); cout << left << "0." << "EXIT" << endl;
cin >> menuChoice;
return menuChoice;
}
int getStudentOption(int numStudents, Student students[]){

int optionNum, i;

do{
cout.width(8); cout << left << "Option";
cout.width(8); cout << left << "First";
cout.width(6); cout << left << "Last" << endl;
cout << "======================" << endl;

for(i = 0; i < numStudents; i++){
cout << i + 1;
cout.width(7); cout << left << ".";
cout.width(8); cout << left << students[i].getFirstName();
cout.width(8); cout << left << students[i].getLastName() << endl;
}
cout << "Choose the student option number: ";
cin >> optionNum;
}while(optionNum > i || optionNum <= 0);
return optionNum;
}
void addCourses(Student student){
int numCourses, number;
string prefix, name;
Course course;
if(student.getNumCourses() < MAX_COURSES){
do{
cout << "How many courses would you like to add? ";
cin >> numCourses;
if(numCourses + student.getNumCourses() > MAX_COURSES){
student.print();
cout << " already has " << student.getNumCourses() << " out of " << MAX_COURSES << " courses" << endl;
}
}while(numCourses + student.getNumCourses() > MAX_COURSES);

if(numCourses + student.getNumCourses() < MAX_COURSES){
for(int i = 0; i < numCourses; i++){
cout << "Enter the course prefix, number, and name: ";
cin >> prefix >> number >> name;
course.setPrefix(prefix);
course.setCourseNum(number);
course.setCourseName(name);
if(student.addCourse(course)){

}
else{

}
}
}
}
else{
student.print();
cout << " has a full schedule" << endl;
}
}

student.cpp文件:

#include <iostream>
#include "student.h"
void Student::setNumber(int newNum){
number = newNum;
}
int Student::getNumber() const{
return number;
}
void Student::setFirstName(string newFirst){
firstName = newFirst;
}
string Student::getFirstName() const{
return firstName;
}   
void Student::setLastName(string newLast){
lastName = newLast;
}
string Student::getLastName() const{
return lastName;
}
void Student::setGpa(float newGpa){
gpa = newGpa;
}
float Student::getGpa() const{
return gpa;
}
int Student::getNumCourses() const{
return numCourses;
}
Course* Student::getSchedule(){
return schedule;
}
Student::Student(){
number = 0;
numCourses = 0;
firstName = "";
lastName = "";
gpa = 0;
}
Student::Student(int newNum, int newNumCourses, string newFirst, string newLast, float newGpa, Course newSchedule[]){
number = newNum;
numCourses = newNumCourses;
firstName = newFirst;
lastName = newLast;
gpa = newGpa;
for(int i = 0; i < MAX_COURSES; i++){
schedule[i] = newSchedule[i];
}
}
Student::Student(const Student& rhs){
number = rhs.number;
numCourses = rhs.numCourses;
firstName = rhs.firstName;
lastName = rhs.lastName;
gpa = rhs.gpa;

for(int i = 0; i < MAX_COURSES; i++){
schedule[i] = rhs.schedule[i];
}
}
void Student::print(){
cout << getFirstName() << " " << getLastName();
}
void Student::printSchedule(){
for(int i = 0; i < numCourses; i++){
cout << schedule[i].getPrefix() << schedule[i].getCourseNum() << ":  " << schedule[i].getCourseName() << endl;
}
}
bool Student::addCourse(const Course& newCourse){
if(numCourses < MAX_COURSES){
schedule[numCourses] = newCourse;
numCourses++;

return true;
}
else{
return false;;  
}
}   

student.h文件:

#include <string>
#include "course.h"
#define MAX_COURSES 7
using namespace std;
#ifndef STUDENT_H
#define STUDENT_H
class Student{
int number, numCourses;
string firstName, lastName;
float gpa;
Course schedule[MAX_COURSES];
public:
//Setters
void setNumber(int);
void setFirstName(string);
void setLastName(string);
void setGpa(float);

//Getters
int getNumber() const;
string getFirstName() const;
string getLastName() const;
float getGpa() const;
int getNumCourses() const;
Course* getSchedule();
//Constructors
Student();
Student(int, int, string, string, float, Course[]);
Student(const Student&);
void print();
void printSchedule();
bool addCourse(const Course&);
};
#endif
void addCourses(Student student){

此函数addCourses()对此student参数进行更改,例如调用其addCourse()方法。

在C++中,默认情况下,函数参数是通过值传递的。这意味着该student参数是传递给该函数的任何Student对象的副本。此addCourses()函数有效地更改传入参数的副本:

addCourses(students[studentOption - 1]);

addCourses()返回时,students[studentOption - 1]保持完全不变,因为值传递了,并且addCourses()对此对象的副本进行了更改。

这里有两种可能的解决方案:

  1. 通过引用传递参数。

  2. 显式地returnaddCourses()修改的Student对象。

此外,我的老师说我们可能不会为numCourses和schedule[]属性。

如果你的老师已经解释了什么是引用,如何使用它们,以及如何通过引用传递参数,那么这显然是你必须做的,这是一个微不足道的更改,代码的其余部分保持原样。否则,你的函数应该return它更新的Student对象,然后返回的对象被保存,类似于:

students[studentOption-1] = addCourses(students[studentOption - 1]);

最新更新