结构异常错误

Unusual bug with structs

在这个程序中我需要从一个文本文件中读入信息,第一行是我正在读的课程的数量来处理信息,接下来的 "chunk" 行是课程的名称课程,最后 "chunk" 行列出了 class 数字,该特定课程是先决条件。所以,

18 //number of courses to read in
CSC111 //course 1
CSC112 //course 2
etc....
2    6  //course 1 is a prereq for course 2 and 6
7    8    9    11    15  //course 2 is prereq for course 7,8,9,11,15
etc....

我想出了标记化这些行的方法,我需要将有关给定课程有多少依赖关系的信息放入 "Course" 结构中,其中一个变量是 "numDependencies." 我有声明了一个包含 N+1 个课程对象的数组(索引从 1 开始,而不是 0)。因此,课程 1 中的 numDepenedencies 应为 2,课程 2 中的 numDepenedencies 应为 5,依此类推。问题是,一旦我到达课程 11,我的 numDependencies 变量就会以某种方式设置为 297796848 之类的值,我不知道为什么。这是我的课程结构和主要

typedef struct Course
{
    int numDependencies;
    int numPrerequisites;
    string name;
    int dependencies[6];
} Course;

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cstdio>
#include "Course.h"
using namespace std; 

int main()
{

    int N;  //Number of courses
    ifstream infile("CSCCourses.txt");
    if (infile.fail()){
        cout << "File not found." << endl;
        //exit(0);
    }
    //read in number of courses
    infile >> N;
    //dynamically allocate array of courses
    Course* courseArray = new Course[N+1];
    //loop N times to read course names
    for (int i = 1; i <= N; i++){
        infile >> courseArray[i].name;
        cout << courseArray[i].name << endl;
    }
    //loop again to read course information
    //string str; //maybe this variable should be declared here?
    for (int j = N; j <= N+12; j++) 
    {
        cout << "Course " << j - N << endl;
        string str;
        getline(infile, str);
        //Skip delimiters
        string::size_type lastPos = str.find_first_not_of(" ", 0);
        //Find first non delimiter
        string::size_type pos = str.find_first_of(" ", lastPos);
        while (string::npos != pos || string::npos != lastPos)
        {
            //Found token, put prereqs in course
            string numAsStr = str.substr(lastPos, pos - lastPos);
            //convert numasstr to integer
            int num = atoi(numAsStr.c_str());
            cout << num << endl;
            //use integer to set values properly in array of courses
            if(num != 0) {
                courseArray[j].numDependencies++;
            }
            lastPos = str.find_first_not_of(" ", pos);
            //find next non delimiter
            pos = str.find_first_of(" ", lastPos);
        }

        int number = courseArray[j].numDependencies;
        cout << "Number of dependencies is " << number << endl;
        cout << "--------------------------" << endl;
    }
infile.close();

}

while 循环之前添加下一个代码:

    courseArray[j].numDependencies = 0;

编辑:

原回答有点错误。我忽略了你是如何分配那个数组的。

接下来是实际问题:您正在分配 N+1 个元素并尝试访问 N+2、N+3 等元素。所以你应该这样做:

Course* courseArray = new Course[N+13];

而不是

Course* courseArray = new Course[N+1];

或像这样更改访问元素代码(注意“-N”代码):

    if(num != 0) {
        courseArray[j-N].numDependencies++;

    int number = courseArray[j-N].numDependencies;

您已将数组标注为 Course* courseArray = new Course[N+1];,但您的第二个 for 循环是 for (int j = N; j <= N+12; j++),这将超出数组的边界。当然,在那之后您立即使用 cout << "Course " << j - N << endl; 修复它,但是您忘记了使用 courseArray[j].numDependencies++;int number = courseArray[j].numDependencies;

再次修复它

但是为什么要将 j 初始化为 N 并进行 13 次迭代?你应该做N!正如我上面所说,使用 j-N 应该可以解决它,但更好的解决方案是通过使其与第一个循环相同来修复第二个 for 循环,如下所示:

for (int j = 1; j <= N; j++){

当然,如果你这样做,你还必须修复你的 cout,使用 j 而不是 j - N