Divinator
Divinator

Reputation: 15

entries of array of an object not updating

I have a class called Random with the following variables

private:
        int Numbers[10];
        int NrHighest;
        int NrLowest;
        int Index;
        int IndexH;
        int IndexL;

and a friend function called friend voidinsertNumbers(Random Random1)`

void insertNumbers(Random Random1)
{
    string line;
    int one,two,three;
    int columOne[10];
    int columTwo[10];
    int columThree[10];
    ifstream myfile("Numbers.dat");
    if (myfile.is_open())
    {
        int i = 0;
        while ( getline (myfile,line) )
        {
            sscanf(line.c_str(),"%i %i %i",&one,&two,&three);
            columOne[i] = one;
            columTwo[i] = two;
            columThree[i] = three;

            i++;
        }
        myfile.close();
    }

    else cout << "Unable to open file";

    switch(run)
    {
        case 0 :
        {
            for(int i = 0;i < 10;i++)
            {
                Random1.Numbers[i] = columOne[i];
                cout << Random1.Numbers[i] << endl;
            };
            break;
        }
        case 1 :
        {
            for(int i = 0;i < 10;i++)
            {
                Random1.Numbers[i] = columTwo[i];
                cout << Random1.Numbers[i] << endl;
            };
            break;
        }
        case 2 :
        {
            for(int i = 0;i < 10;i++)
            {
                Random1.Numbers[i] = columThree[i];
                cout << Random1.Numbers[i] << endl;
            };
            break;
        }
    }

    run ++;
};

I have a cout << Random1.Numbers[i] << endl; to check if the numbers are saved to Random1.Numbersand the output is Output

But the problem come when I try to display the objects here

 cout << Random1;
 cout << Random2;
 cout << Random3;

calling the overloaded function which is also a friend function friend ostream &operator<<( ostream &output,const Random & Random1);

ostream &operator<<( ostream &output,const  Random & Random1)
{
    for(int i = 0;i<10;i++)
    {
        cout << Random1.Numbers[i] << " ";
    }
    cout << endl << Random1.NrHighest << endl << Random1.NrLowest << endl << Random1.Index << endl << Random1.IndexH << endl << Random1.IndexL << endl;
    return output;
};

I get the Defauts values set here

Random()
        {
            Numbers = {0,0,0,0,0,0,0,0,0,0};
            NrHighest = 0;
            NrLowest = 0;
            Index = 0;
            IndexH = 0;
            IndexL = 0;
        };

Rather than the new vaules, heres the output of the overloaded operator<< function Output

I can seem to figure out why the objects doesn't get updated.If you neem more info please ask. Thanks in advance.

Upvotes: 1

Views: 244

Answers (1)

Albert Pumarola
Albert Pumarola

Reputation: 656

In your function void insertNumbers(Random Random1) you are adding values to Random1 but you are passing it by value. Hence, when this function is called with a Random instance, it makes a copy of it, adds values to it and finally destroys it. You will solve this problem by passing Random1 by reference: void insertNumbers(Random &Random1)

Upvotes: 1

Related Questions