int main() { int n; cin>>n>>endl; int a[n]; float b[n]; double c[n]; 这么声明数组为什么不行

#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin>>n>>endl;
int a[n];
float b[n];
double c[n];
void sort(int [],int n);
void sort(float [],int n);
void sort(double [],int n);
sort(a,n);
sort(b,n);
sort(c,n);
return 0;
}

void sort(int a[],int n)
{int i,j,t;
for (j=0;n<5;j++)
for(i=0;n<5-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"输入整型数字:"<<endl;
for(i=0;i<5;i++)
cout<<a[n]<<" ";
cout<<endl;
}

void sort(float a[],int n)
{int i,j;
float t;
for (j=0;j<n;j++)
for(i=0;i<n-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"输入单精度数字:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}

void sort(double a[],int n)
{int i,j;
double t;
for (j=0;j<n;j++)
for(i=0;i<n-j;i++)
if (a[i]>a[i+1])
{t=a[i];a[i]=a[i+1];a[i+1]=t;}
cout<<"输入双精度数字:"<<endl;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
}
要动态声明要这样写
int n;
cin>>n>>endl;
int* a=new int[n];
float* b=new float[n];
double* c=new double[n];
改了以后
还显示
error C2679: binary '>>' : no operator defined which takes a right-hand operand of type '' (or there is no acceptable conversion)
第6行有错

要动态声明要这样写
int n;
cin>>n>>endl;
int* a=new int[n];
float* b=new float[n];
double* c=new double[n];

不过用完之后要记得delete.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-10-13
简单说就是你是在运行时才告诉系统你要的数组的大小,但是系统需要在编译时就知道你要多大空间。

如果想动态定义数组的大小建议使用vector
第2个回答  2010-10-14
好好看书,写错了。本回答被提问者采纳