2014-09-30 47 views
0

我在下面的程序中編寫了這個程序,通過從用戶獲取i/p來打印數組,但顯示編譯錯誤。我想要一個程序以升序和降序打印這些輸入。在C++中以升序和降序打印數組值

#include <iostream> 

using namespace std; 
int main() 
{ 
    int a[10] = {}; // ![enter image description here][1] 
    for (int i = 0; a[i] <= 9; i++) 
     { 
     cout << "Enter the numbers:" << endl; 
     cin >> a[i]; 
     } 
    cout << a[i] << endl; 
} 

錯誤:

![error by codeblocks][1] 

||=== Build: Debug in example (compiler: GNU GCC Compiler) ===| 
H:\c++\example\main.cpp||In function 'int main()':| 
H:\c++\example\main.cpp|12|error: name lookup of 'i' changed for ISO 'for' scoping [-fpermissive]| 
H:\c++\example\main.cpp|12|note: (if you use '-fpermissive' G++ will accept your code)| 
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===| 
+0

最新錯誤?你的排序代碼在哪裏? – Borgleader 2014-09-30 17:58:39

+0

你應該把錯誤的文字放在這裏,以便我們看到它。 – crashmstr 2014-09-30 17:58:47

+0

打印出來時也需要一個循環。 – 2014-09-30 17:58:53

回答

2

你應該在一個循環打印數字,也是你的循環狀況似乎是錯誤的。這將工作:

#include <iostream> 

using namespace std; 
int main() 
{ 
    int a[10]; 
    int i; 
    cout << "Enter the numbers:" << endl; 

    for (i = 0; i <= 9; i++) 
    { 
     cin >> a[i]; 
    } 
    for (i = 0; i <= 9; i++) 
    { 
     cout << a[i] << endl; 
    } 

} 

如果你想顯示他們排序,你必須先使用sort()。

+0

非常感謝你:) – Frankline 2014-09-30 18:17:58