结构的不寻常的错误

时间:2015-02-20 00:09:29

标签: c++ for-loop struct tokenize

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

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....

我已经想出要对行进行标记,我需要将有关给定课程有多少依赖关系的信息放入"课程" struct,其中一个变量是" numDependencies。"我已经声明了一个N + 1个过程对象的数组(索引从1开始,而不是0)。因此,课程1中的numDepenedencies应为2,课程2中的numDependencies应为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();

}

2 个答案:

答案 0 :(得分:0)

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;

答案 1 :(得分:0)

您已将数组标注为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循环,如下所示:

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

当然,如果你这样做,你还必须使用j代替j - N修复你的cout。