游戏王天空龙什么梗:这个程序哪里有错啊 ?

来源:百度文库 编辑:高校问答 时间:2024/05/05 23:47:45
这个程序哪里错啊?
#include <iostream.h>
#include <string.h>
class Stud
{
int no;
char name[10];
int score;
public:
void Set(int _no,char _name[],int _score)
{
no=_no;
strcpy(name,_name);
score=_score;
}
void disp()
{
cout<<no<<" "<<name<<" "<<score<<endl;
}
};
void main()
{
int n,_no,i;char _name[10];int _score;
cout<<"输入学生数目:";
cin>>n;
Stud st[n];
for(i=1;i<=n;i++)
{
cout<<endl<<"输入第"<<n<<"个学生的学号:";
cin>>_no;
cout<<endl<<"输入第"<<n<<"个学生的姓名:";
cin>>_name;
cout<<endl<<"输入第"<<n<<"个学生的分数:";
cin>>_score;
st[i].Set(_no,_name,_score);
cout<<endl;
}
for(i=1;i<=n;i++)
{
st[i].disp();
}
}

Stud st[n];错误;
数组元素个数必须是常量,若想用元素个数不固定的数组,必须动态分配空间,
Stud st*;
st=new Stud[n];
当然相应的st[i].改为st[i]->

另外cout<<endl<<"输入第"<<n<<"个学生的学号:"; 中的n应该是i