未处理的异常错误c ++

时间:2015-03-23 09:36:22

标签: c++

我试图通过从文件中获取行将文本文件存储到多维数组中,将它们存储在变量中,然后将这些变量添加到数组中。

string QUESTIONS[3][49];

int x;
ifstream myFile;
myFile.open("Questions.txt");

getline(myFile, tempNum);
questionNum = stoi(tempNum);

for (int x = 0; x == 49; x++)
{
getline(myFile, line1);
getline(myFile, line2);
getline(myFile, line3);
getline(myFile, line4);
getline(myFile, tempNum);
correctAns = stoi(tempNum);

QUESTIONS[x][0] = line1;
QUESTIONS[x][1] = line2;
QUESTIONS[x][2] = line3;
QUESTIONS[x][3] = line4;

x++;
}

Ass1.exe中0x0FB231CA(msvcr120d.dll)的未处理异常:0xC0000005:访问冲突写入位置0xCCCCCCCC。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

我认为解决办法可能是:

string QUESTIONS[50][4];

int x;
ifstream myFile;
myFile.open("Questions.txt");

getline(myFile, tempNum);
questionNum = stoi(tempNum);

for (int x = 0; x < 50; x++)
{
getline(myFile, line1);
getline(myFile, line2);
getline(myFile, line3);
getline(myFile, line4);
getline(myFile, tempNum);
correctAns = stoi(tempNum);

QUESTIONS[x][0] = line1;
QUESTIONS[x][1] = line2;
QUESTIONS[x][2] = line3;
QUESTIONS[x][3] = line4;

}

你有两次增加x,并且字符串数组没有以正确的顺序声明。