C++:尝试打印 const char* 数组时程序崩溃

标签 c++ arrays crash exit-code const-char

我有一个程序,让用户输入他们的姓名、年龄和他们参加的类(class)。这些类作为二维字符数组存储在 main() 函数中,然后传递给 Student 类中的函数,该函数将该数组复制到名为 m_CourseNames 的公共(public)成员 const char* 数组中。我已使用 CLion 中的调试器验证此过程已成功完成。

但是,每当我尝试迭代 m_CourseNames 数组并将其内容打印到屏幕上时,我都会收到退出代码 11 并且程序崩溃。

我尝试通过以下方式将数组打印到屏幕:

  1. 在主函数中:

    for (int count = 0 ; count < 9 ; count++)
        cout << student.m_CourseNames[count] << " ";
    
  2. 在学生类的成员函数中,通过student.getCourses();调用:

    for (int count = 0 ; count < 9 ; count++)
        cout << m_CourseNames[count] << " ";
    
  3. 在重载运算符函数内,其方法与 1) 中相同(请参阅代码以了解我为何在此处尝试)

在程序完成之前尝试将 const char* 数组打印到屏幕的所有三种方法都会导致以下错误代码:

如果数组是公共(public)成员变量,不知道为什么它不会迭代。调试器验证所有类都正确存储在其中,因此它不是 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;
}

最佳答案

您正在迭代数组中尚未填充的条目。你想要这样的东西:

void Student::getCourses()
{
    cout << ", Courses: ";
    for (int count = 0 ; count < index ; count++)
        cout << m_CourseNames[count] << " ";
}

此外,正如 Arun 指出的那样,您正在越界访问数组。您无法访问十个条目数组的第十一个元素,条目 10 是第十一个元素(因为 0 是第一个元素)。

由于多种原因,您的代码确实很糟糕。主要问题是您的 Person 类存储了指向它不拥有的内存的指针,这使得该类非常难以使用。为什么不使用std::string

关于C++:尝试打印 const char* 数组时程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32387577/

相关文章:

Python 程序运行一周,然后莫名其妙地失败了

c++ - 执行 switch 语句中的所有情况 - 具体问题

java - 使用数组的元素填充另一个数组 - Java

c - 查找数组中给定字符串的索引

PHP CodeIgniter - 当关系表(不同表中有相同的列名)时如何获得良好的结果数组

haskell - WinGHCi 无法启动

android - 不幸的是,相机在使用 Content Provider 时停止了”

c++ - VS2013 中的类型特征

c++ - 静态 vector 成员变量

c++ - 在 qmake 中添加了编译器选项的附加 makefile 目标