我有一个程序,让用户输入他们的姓名、年龄和他们上过的课程。这些类作为二维字符数组存储在 main() 函数中,然后将它们传递给 Student 类中的函数,该函数将该数组复制到名为 m_CourseNames 的公共成员 const char* 数组中。我已使用 CLion 中的调试器验证此过程已成功完成。
但是,exit code 11
每当我尝试遍历 m_CourseNames 数组并将其内容打印到屏幕上时,程序就会崩溃。
我尝试通过以下方式将数组打印到屏幕上:
在主函数中:
for (int count = 0 ; count < 9 ; count++) cout << student.m_CourseNames[count] << " ";
通过调用 via 在学生类的成员函数中
student.getCourses();
:for (int count = 0 ; count < 9 ; count++) cout << m_CourseNames[count] << " ";
在一个重载的运算符函数中,使用与 1) 中相同的方法(请参阅代码以了解我为什么在这里尝试它)
在程序完成之前,尝试将 const char* 数组打印到屏幕的所有三种方式都导致以下错误代码:
如果数组是公共成员变量,不知道为什么不会迭代。调试器验证所有类都正确存储在其中,因此它不是 addCourses() 函数的责任。
请参阅下面的整个程序(注释掉的所有内容都是尝试将数组打印到屏幕上):
--main.cpp--
#include <iostream>
#include "Student.h"
using namespace std;
int main()
{
char input[10][128] = {(0),(0)};
char name[128] = {0}, student_check = ' ';
int age = 0, count = 0;
cout << "\nPlease state your name and age:\n\n";
cout << "Name: ";
cin.getline(name, 128);
cout << "Age: ";
cin >> age;
cin.clear();
cin.ignore();
cout << "\n\nThanks!\n\nAre you a student? (Y/N): ";
cin.get(student_check);
switch (student_check)
{
case 'y':
case 'Y':
{
Student student;
student.setName(name);
student.setAge(age);
char course_check = ' ';
cout << "\n\nWhat course(s) are you taking?"
<< " (Enter the course prefix and number without any spaces): \n\n";
while (tolower(course_check) != 'n') {
cin.clear();
cin.ignore();
cout << "Course #" << count + 1 << ": ";
cin.getline(input[count], 128);
student.addCourse(input[count], count);
if (student.addCourse(input[count], count))
{
cin.clear();
cout << "Do you want to enter another course? (Y/N): ";
cin.get(course_check);
count++;
}
else
{
cout << "You have exceeded the number of courses you are allowed to enter" << endl << endl;
course_check = 'n';
}
cout << student;
student.getCourses();
//for (int count = 0 ; count < 9 ; count++)
// cout << student.m_CourseNames[count] << " ";
}
}
default:
break;
}
}
--Student.h---
#ifndef PA2_STUDENT_H
#define PA2_STUDENT_H
#include "Person.h"
#include <ostream>
class Student : public Person
{
public:
Student();
Student(const char* []);
bool addCourse(const char*, int);
void getCourses();
friend std::ostream& operator <<(std::ostream& os, const Student& student);
const char* m_CourseNames[10];
};
#endif
--student.cpp--
#include "Student.h"
#include <iostream>
using namespace std;
Student::Student() {}
Student::Student(const char* m_CourseNames[])
{
m_CourseNames[10] = {0};
}
bool Student::addCourse(const char* course, int index)
{
if (index < 9)
{
m_CourseNames[index] = course;
return true;
}
if (index >= 9)
return false;
}
void Student::getCourses()
{
cout << ", Courses: ";
for (int count = 0 ; count < 9 ; count++)
cout << m_CourseNames[count] << " ";
}
std::ostream &operator<<(std::ostream& os, const Student& student) {
os << "Name: " << student.m_Name << ", Age: " << student.m_Age;// << ", Courses: " << Student::m_CourseNames;
//cout << ", Courses: ";
// for (int count = 0 ; count < 9 ; count++)
// cout << m_CourseNames[count] << " ";
return os;
}