结构异常错误

Unusual bug with structs

本文关键字:错误 异常 结构      更新时间:2023-10-16

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

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的numDependencies应该为5,依此类推。问题是,一旦我达到课程11,我的numDeperencies变量就会被设置为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