关于类的问题
关于学生的类包括成绩 学号 名字。名字学号军用char 型。
执行时,再输完成绩后在屏幕上出现的学号是一堆乱码!
求大神指出错误!!!
#include<iostream.h>
#include<string.h>
class student
{
char name[8];
char *no;
double score[4];
public:
student()
{
char num[8]="203";
cout<<"调用缺省值构造函数"<<endl;
strcpy(name,"xiang");
no=num;
for(int i=0;i<4;i++)
score[i]=60;
}
student(char na[],char n[],double s[])
{
cout<<"调用构造函数"<<endl;
strcpy(name,na);
if(n)
{
no=new char [(strlen(n)+1)];
strcpy(no,n);
}
for(int i=0;i<4;i++)
score[i]=s[i];
}
~student()
{
cout<<"调用析构函数"<<endl;
if(no) delete []no;
}
student(student &p)
{
int i;
cout<<"调用拷贝构造函数"<<endl;
strcpy(name,p.name);
if(p.no)
{
no=new char [strlen(p.no)+1];
strcpy(no,p.no);
}
for(i=0;i<4;i++)
score[i]=p.score[i];
}
void setnameno(char *na,char *no);
void setscore(double s[]);
double total();
double average();
void outputone();
void modiscore(char c,double s);
};
void student::setnameno(char *na,char *n)
{
strcpy(name,na);
strcpy(no,n);
}
void student:: setscore(double s[])
{
int i;
for(i=0;i<4;i++)
score[i]=s[i];
}
double student:: total()
{
double sum=0;
int i=0;
for(i=0;i<4;i++)
sum+=score[i];
return sum;
}
double student:: average()
{
double aver=0;
int i;
for(i=0;i<4;i++)
aver+=score[i];
aver=aver/4;
return aver;
}
void student:: outputone()
{
int i;
cout<<"名字:"<<name;
cout<<"学号:"<<no;
for(i=0;i<4;i++)
switch(i)
{
case(0):cout<<"数学成绩:"<<score[i]<<endl;break;
case(1):cout<<"英语成绩:"<<score[i]<<endl;break;
case(2):cout<<"物理成绩:"<<score[i]<<endl;break;
case(3):cout<<"C++成绩:"<<score[i]<<endl;break;
}
}
void student:: modiscore(char c,double s)
{
switch(c)
{
case('m'):score[0]=s;break;
case('e'):score[1]=s;break;
case('p'):score[2]=s;break;
case('c'):score[3]=s;break;
}
}
void main()
{
char name[8],no[8],*p,*p0;
p=name;
p0=no;
double score[4];
int i;
student p1;
cout<<"请输入名字和学号:"<<endl;
cin>>name>>no;
p1.setnameno(p,p0);
cout<<"请输入成绩:"<<endl;
for(i=0;i<4;i++)
cin>>score[i];
p1.setscore(score);
p1.outputone();
cout<<"总成绩为:"<<p1.total()<<endl;
cout<<"平均成绩为:"<<p1.average()<<endl;
p1.modiscore('m',86);
p1.outputone();
}