C ++:满足循环条件时程序崩溃

C++: Program crashes when loop condition is met

我有一个程序可以根据用户输入从两个不同的 classes 创建对象。如果用户是学生,将创建学生 class 的对象,学生将在其中输入他们正在学习的 classes。我有一个 while 循环,在每次输入 class 后询问用户是否要输入另一个 class。如果此人键入 n,这是应该结束循环的条件,程序将以 exit code 11 停止:

这不应该是这样的。 while循环后面的代码行比较多,循环结束后程序应该还没有结束。这是有问题的 while 循环的函数:

void createStudent (char student_name[], int student_age)
{
    Student student;

    student.setName(student_name);
    student.setAge(student_age);

    char courses[8];
    char course_loop = ' ';
    int count = 0;

    cout << "What courses are you taking? "
        "(Enter course prefix and number with no spaces):\n\n";

    while (tolower(course_loop) != 'n')
    {
        cout << "Course #" << count + 1 << ": ";
        cin.ignore();
        cin.getline(courses, 9);
        //student.sizeOfArray(); // Increment the array counter if addCourse reports that the array was not full
        student.addCourse(courses, count);

        cin.clear();

        if (student.addCourse(courses, count))
        {
            cout << "\nHave another course to add? (Y/N): ";

            cin.clear();

            cin.get(course_loop);
        }
        else
        {
            cout << "You have exceeded the number of courses you're allowed to enter. Press any ENTER to continue...";
            cin.ignore();
            course_loop = 'n';
        }

        count++;
    }

    cout << student;
    student.printCourseNames();
}

这是程序的其余部分:

// main.cpp
//-----------------------

#include <iostream>
#include "Person.h"
#include "Student.h"
using namespace std;

void createStudent(char [], int);
void createPerson(char [], int);

int main()
{
    char name[128], student_check;
    int age;

    cout << "Please state your name and age: \n\n"
         << "Name: ";
    cin.getline(name, 128);
    cout << "Age: ";
    cin >> age;

    cout << "\n\nThanks!\n\nSo are you a student? (Y/N):";
    cin.ignore();
    cin.get(student_check);

    switch (student_check)
    {
        case 'y':
        case 'Y':
            createStudent(name, age);
            break;
        case 'n':
        case 'N':
            createPerson(name, age);
            break;
        default:
            break;
    }
}

// createStudent function with while-loop posted above comes after this in main.cpp

// student.h
// ------------------

#include "Person.h"
#ifndef PA2_STUDENT_H
#define PA2_STUDENT_H


class Student : public Person
{
    public:
        Student();
        bool addCourse(const char*, int);
        void printCourseNames();
        void sizeOfArray();

    private:
        const char* m_CourseNames[10] = {0};
        int array_counter;
};


#endif

// student.cpp
//------------------

#include <iostream>
#include "Student.h"

using namespace std;

Student::Student() : array_counter(0) {}

void Student::sizeOfArray()
{
   array_counter++;
}

bool Student::addCourse(const char* course, int  index)
{
    if (index < 9)
    {
        m_CourseNames[index] = course;
        return true;
    }
    else if (index == 9)
        return false;
}

void Student::printCourseNames()
{
    if (array_counter != 0)
    {
        cout << ", Courses: ";

        for (int count = 0 ; count < 10 ; count++)
            cout << m_CourseNames[count] << " ";
    }
}

我正在使用 CLion 作为我的 IDE,如果有帮助的话。

编辑:与 return 相关的错误与缓冲区有关。

您的输入缓冲区 char courses[8] 不够大,无法处理来自 cin.getline(courses, 9) 的输入,这会导致 cin 缓冲区溢出。 cin.clear() hack 正在恢复输入流以允许输入 cin.get(course_loop),但当函数尝试 return 时异常仍未处理。

char courses[8] 更改为 char courses[10](以匹配 Student 中的 m_CourseNames 数组),它应该可以正常工作。

更新代码:

void createStudent(char student_name[], int student_age)
{
    Student student;

    student.setName(student_name);
    student.setAge(student_age);

    char courses[10];
    char course_loop = ' ';
    int count = 0;

    cout << "What courses are you taking? "
        "(Enter course prefix and number with no spaces):\n\n";

    while (tolower(course_loop) != 'n')
    {
        cout << "Course #" << count + 1 << ": ";
        cin.ignore();
        cin.getline(courses, 9);
        cin.clear();

        //Removed the duplicate addCourse() call
        if (student.addCourse(courses, count))
        {
            student.sizeOfArray();  //Needed for printCourseNames() functionality
            cout << "\nHave another course to add? (Y/N): ";

            cin.clear();

            cin.get(course_loop);
        }
        else
        {
            cout << "You have exceeded the number of courses you're allowed to enter. Press any ENTER to continue...";
            cin.ignore();
            course_loop = 'n';
        }

        count++;
    }

    cout << student;
    student.printCourseNames();

    return;
}

您需要添加一个函数,可以将 Student 对象发送到 cout 等输出流。实现这一点的方法是定义一个像这样的函数:

ostream& operator<<(ostream& os, const Student& student)
{
  os << "Name: " << student.name << ", Age: " << student.age << ", Courses: " << student.m_CourseNames;
  return os;
}

此函数允许您使用 << 运算符将数据从您的 Student class 发送到输出流,例如 cout。它将输出流作为一个参数,将学生作为另一个参数。它打印学生的信息,然后 returns 输出流。这样您就可以链接运算符以便一次打印多条信息。