我在下面的程序中编写了这个程序,通过从用户那里获取i / p来打印数组,但是显示了一个编译错误。我希望程序按升序和降序打印输入。
#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 :(得分: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()。