-1

I want float variable 'avg' to be returned and then in the main pass it to 'void batsman::display(float a)' using a parameter. then display the average marks in display method. this method gives me 2 errors. any other way?

#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";
}
  • 2
    *"this method gives me 2 errors."* - Wouldn't it be helpful, if you told use, what they are? – IInspectable Aug 06 '16 at 17:52
  • 2
    I dont know what errors you got but you'll want to replace the `::` with `.` in the line `b1.display(b1.batsman::cal());` – David Aug 06 '16 at 17:53
  • 1
    When posting question about build errors, always include the exact errors you get, complete, in full, unedited, and including any informational notes. They should be in the body of the question, as text. Preferably copy-pasted. Also point out on which lines you get the errors, for example by comments in the code you show. – Some programmer dude Aug 06 '16 at 18:04

2 Answers2

1

The code has several errors.

  1. iostream.h should be iostream
  2. using namespace std; // add this at top so that cout is found
  3. display() should be display(float a) in the class declaration.

After those changes the code ran as expected.

Matthew Fisher
  • 2,258
  • 2
  • 14
  • 23
  • thank you very much. You gave me an answer i've been searching for months.. ! THnx! –  Aug 06 '16 at 18:40
  • 1
    @SiL3n7neo If you have been searching for months, then you have been searching in the wrong places. [Any good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) would have helped you with the first two things. The third should have been solved by just looking at the code and actually reading the error messages from the compiler. – Some programmer dude Aug 06 '16 at 20:15
0

Change display's definition in your class to display(float a), and prepend std:: to cout and cin. As a recommendation, don't use iostream.h, use iostream.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88