2017-06-22 82 views
-2

我已經創建了一個數組,但現在它的大小需要由文件中的行數決定,直到我使用for循環來放置數組元素在另一個文件中,我得到錯誤「_Pnext是0xCCCCCCD0」,爲什麼?如果我把一個數,即30在for循環,而不是NUMBER_OF_LINES,其優良工程動態數組大小與文件中行數的差異

int number_of_lines=0; 
std::string line2; 
std::ifstream myfile("Input_files\\Pronunciations.txt"); 
while (std::getline(myfile, line2)) 
{ 
    ++number_of_lines; 
} 
string *hexsource = new string[number_of_lines]; 
for (int o = 0; o <number_of_lines; ++o) 
{ 
    file2 >> name[o] >> ipaname[o] >> anyway[o]; 
} 
+0

使用'std :: vector '。不要'新的std :: string',它是一個值類型,就這樣使用它。 – StoryTeller

+1

'o <++ number_of_lines'似乎是可疑的。 – Jarod42

+0

'hexsource'未使用,我們沒有聲明'file2','name','ipaname'和'anyway'。 – Jarod42

回答

0

你確定問題出現在循環中嗎? 如果是這樣的話,我可以考慮的一個問題是,如果number_of_lines比name/ipname /的大小還要大,它會產生問題。

你給這些的尺寸是多少?

number_of_lines的值最大值是多少?

您需要提及代碼中的每個必需事物

+0

number_of_lines的值是302,文件中的文本正在加載到字符串hexsource [number_of_lines]中。如果在for循環中,我說for(int o = 0; o <302; ++ o),那麼它可以很好地工作 – Znap

+0

那麼,如果通過將number_of_lines改爲302,程序運行良好,那就是說number_of_lines的值大於302 –

+0

添加一行: 「std :: cout << number_of_lines << endl;」 之前的for循環確認 –

0
for (int o = 0; o <++number_of_lines; ++o) 

也許應該

for (int o = 0; o < number_of_lines; ++o) 

否則你的結束條件永遠都無法滿足。

+0

對不起,我在這裏犯了一個非常愚蠢的錯字,它不是<++ number_of_lines,只是 Znap