2016-12-14 35 views
1
#include<iostream> 
using namespace std; 
void arrayin(int x[], int n); 
void arrayout(int x[], int n); 
main() 
{ 
    int n, x[n]; 
    cout << "Please enter the number of elements in the array: " << endl; 
    cin >> n; 
    cout << "Please enter the elements: " << endl; 
    arrayin(x,n); 
    cout << "Array is of " << n << " elements."<< endl; 
    cout << "Elements are as follow :" << endl; 
    arrayout(x,n); 
} 
void arrayin(int x[],int n) 
{ 
    for (int i = 0; i < n; i ++) 
    { 
     cin >> x[i]; 
    } 
} 
void arrayout(int x[], int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
     cout << x[i] << "\t"; 
    } 
} 

我是新來的編程。 它崩潰超過8個元素,如果n> 8崩潰..但對於n < 8工作正常.. 不知道爲什麼!動態陣列崩潰超過8個元素

+3

'INT N','X [N];'--->未定義的行爲 – LPs

+0

我不不知道,C++支持VLA?我認爲這更多的是C的東西,但我可能是錯的。 –

+2

你把它編譯爲C++嗎? (因爲你在知道'n'之前聲明'x [n]',所以這並不重要)。可變長度的數組是一個'C'的東西 - 在C++中我會使用'vector'。 – doctorlove

回答

0

在輸入n後聲明你的數組。

#include<iostream> 
using namespace std; 
void arrayin(int x[], int n); 
void arrayout(int x[], int n); 
main() 
{ 
    int n; 
    cout << "Please enter the number of elements in the array: " << endl; 
    cin >> n; 
    int x[n]; 
    cout << "Please enter the elements: " << endl; 
    arrayin(x,n); 
    cout << "Array is of " << n << " elements."<< endl; 
    cout << "Elements are as follow :" << endl; 
    arrayout(x,n); 
} 
void arrayin(int x[],int n) 
{ 
    for (int i = 0; i < n; i ++) 
    { 
     cin >> x[i]; 
    } 
} 
void arrayout(int x[], int n) 
{ 
    for (int i = 0; i < n; i++) 
    { 
     cout << x[i] << "\t"; 
    } 
} 

此代碼有效。問題是,當你爲n聲明一個沒有值的數組時,數組將被過去的堆棧中的任何東西初始化。可變長度數組需要這個值。所以稍後再聲明這個數組。

有人沒有任何理由downvoted。編輯:變長數組不是ISO C++的一部分。要編寫符合標準的C++代碼,您應該使用帶有g ++或clang的-pedantic標誌。

你有兩個簡單的選擇。對於固定長度數組使用std :: array或對動態數組使用std :: vector。

+3

或..使用'std :: vector ' – WhozCraig

+0

謝謝,它的工作。 – Qasimi

+2

我用一個理由downvoted ..你的問題,編輯前,包括「聲明你的陣列輸入n後」。這不是一個真正的答案,它是一個評論。在您編輯之後,我將撤回我的downvote。但是IMO這個答案沒有用,因爲你沒有提到非標準擴展。甚至是一個真正的C++解決方案 –

5

這裏的問題是:

int n, x[n]; // It is undefined behaviour 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 

正確的方法是(你的編譯器與可變大小陣列擴展):

int n; 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
int x[n]; 

使用C++的正確的方法是使用std::vector相反:

int n; 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
std::vector<int> x(n); 

並且您必須進行一些其他更改以適應std::vector

3

的問題是在這裏:

int n, x[n]; // <-- n is not yet initialized 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
cout << "Please enter the elements: " << endl; 
arrayin(x,n); 

你需要這樣的:

int n; 
cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
int x[n]; // << now n has been initialized 
cout << "Please enter the elements: " << endl; 
arrayin(x,n); 

BTW:VLA(或者你給他們打電話動態數組)是在C++中不標準,但海灣合作委員會(和possibily鏗鏘)將它們作爲擴展。

2

int n, x[n];是您聲明n,將有一個不確定的值問題

。有了這個值,你正在聲明一個數組,它將有一個不確定的大小。

您正在使用C++,因此使用new關鍵字創建你的陣列,用戶輸入之後:

cout << "Please enter the number of elements in the array: " << endl; 
cin >> n; 
int *x = new int[n]; 
// your stuff 
delete x;