redcat
redcat

Reputation: 3

Access a value from a separate function

class FinalList {
    Location List[20];
    int MaxSize;
    int Size;


public:
    FinalList()
    {
        MaxSize = 20;
    }

    void RunIt();
    void Show();
    void Mean();
    void Menu();
};


void FinalList::Mean()
{
    int K;
    double Total;
    double Average;

    for(K=0 ; K < Size ; K++)
        Total += List[K].Value;

    Average = Total / Size ;
    cout << "Average: " << Average << endl;
}

void FinalList::Show()
{
    int Count;

    for(Count = 0; Count < Size ; Count++)
        List[Count].Display();

    cout << "Average: " << Average << endl;
}

I can calculate the average value from the array in Mean(). But how can I later access this value in Show()?

Upvotes: 0

Views: 112

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 406025

You can either store it in an instance variable (like you're doing with MaxSize) or make Mean() return the average value instead of just printing it out.

If you make Mean() return the average, then you can call it in your Show() function like this:

cout << "Average: " << Mean() << endl;

Upvotes: 4

Related Questions