我希望返回浮点变量'avg',然后在main中使用参数将其传递给'void batsman :: display(float a)'。然后在显示方法中显示平均标记。这个方法给了我2个错误。任何其他方式?
#include<iostream.h>
class batsman
{
int marks[5];
char name[15],country[15];
public:
void input();
float cal();
void display();
};
void batsman::input()
{
int i;
cout<<"Enter player name: ";
cin>>name;
cout<<"Enter player country: ";
cin>>country;
cout<<"Enter player marks"<<"\n";
for(i=0;i<5;i++)
{
cout<<"Mark "<<i+1<<": ";
cin>>marks[i];
}
}
float batsman::cal()
{
int i;
int tot=0;
float avg;
for(i=0;i<5;i++)
{
tot=tot+marks[i];
}
avg=(float)tot/5;
return avg;
}
void batsman::display(float a)
{
float avg1;
avg1=a;
cout<<"Player name: "<<name<<"\n";
cout<<"Player country: "<<country<<"\n";
cout<<"Average: "<<avg1<<"\n";
}
int main()
{
batsman b1;
b1.input();
b1.cal();
b1.display(b1.batsman::cal());
//cout<<"Average: "<<b1.batsman::cal()<<"\n";
}
答案 0 :(得分:1)
代码有几个错误。
在这些更改之后,代码按预期运行。
答案 1 :(得分:0)
将班级中的display
定义更改为display(float a)
,并将std::
添加到cout
和cin
。建议不要使用iostream.h
,请使用iostream
。