2014-03-26 57 views
0

好的,很抱歉地提出另一個這樣的問題。我查看了答案的答案。我得到了圍繞變量nums的錯誤堆棧。是否意味着我在變量nums附近鍵入了一些額外的代碼?我試圖找出它,但我找不到它。我是編碼的小白,所以很抱歉。這裏是我的代碼,我認爲問題在於我的運行時檢查錯誤2

cout << "The numbers on file are:\n " << nums, size; 

和我上面的主要read_data調用,但我找不到問題。謝謝!

我的代碼:

#include <iostream> 
#include <fstream> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

void read_data(int nums[], int size); 

int main() 
{ 
const int size = 24; 
ifstream dataIn; 
int nums[size]; 

    read_data(nums,size); 

    cout << "The numbers on file are:\n " << nums, size; 

system("PAUSE"); 
return 0; 
} 

void read_data(int nums[], int size) 
{ 
    ifstream dataIn; 
    dataIn.open("walrus.txt"); 

    if(dataIn.fail()) 
      { 
        cout << "File does not exist." << endl; 
        exit(1); 
      } 

    int count; 
    for (count = 0; count < size; count++) 
    { 
      dataIn >> nums[size]; 
    } 

    dataIn.close(); 
} 
+1

問題是什麼? – aschepler

回答

2

關於運行時的問題 -

dataIn >> nums[size]; 

嘗試訪問數組索引出界是不確定的行爲。大小爲N的有效數組索引是至N-1

+0

@ user3267868另外...圍繞變量「數」堆棧?也許你的意思是這個數字在棧內存中。 – Claudiordgz

+0

即使'nums'在堆棧上,這種情況下也不是問題。 'nums'的生存時間直到'main'返回。 – Mahesh

+0

我知道,我無法圍繞他的問題「圍繞變量」編號「」 – Claudiordgz