0

I have two dynamically allocated array of class objects - student and staff. When a user inputs the age, based on the age I would like to either update the elements of student array or staff array. But my code below doesn't work. The variable person doesn't get reassigned to staff once assigned to student. All the data I enter goes into student only no matter the age I enter. What is wrong with my code? How can I have one variable and assign it either one or the other array elements based on a condition check?

#include <iostream>
using namespace std;

int main()
{
    class info
    {
    public:
        int unique_id;
        char* hair_color;
        int height;
        int weight;
    };

    class info* student;
    student = new info[10];

    class info* staff;
    staff = new info[10];

    for (int i=0; i<10;i++)
    {
        class info& person = student[i];

        int age ;
        cout<< "enter age"<<endl;
        cin >> age;

        if( age > 18 )
        {
            person = staff[i];  // This assignment doesn't work ??
        }
        cout<< "enter unique_id"<<endl;
        cin >> person.unique_id;
        cout<< "enter height"<<endl;
        cin >> person.height;
        cout<< "enter weight"<<endl;
        cin >> person.weight;

    }

    cout<<" Student "<<student[0].unique_id<<"   "<<student[0].height<<"\"  "<<student[0].weight<<endl;
    cout<<" Staff "<<staff[0].unique_id<<"  "<<staff[0].height<<"\"  "<<staff[0].weight<<endl;

    return 0;
}
user8642915
  • 1
  • 1
  • 3
  • Looks like C++ code. If so, please add the C++ tag to your question. Thanks! – Louis Langholtz Sep 25 '17 at 18:11
  • If you have a reference `int& rx = x;` what do you think should happen if you assign new value to the reference like this `rx = 5;`? And what is happening in the line where you think assignment does not work? – Artemy Vysotsky Sep 25 '17 at 18:43
  • You cannot [reseat a reference.](https://stackoverflow.com/questions/7713266/how-can-i-change-the-variable-to-which-a-c-reference-refers) This leads to the problems @ArtemyVysotsky hints at. – user4581301 Sep 25 '17 at 18:47
  • If your instructor has not explicitly disallowed using `std::vector`, use it in place of `info* student;` and `info* staff;`. You don't have to write `class info* student;` by the way. The compiler's not dumb. It knows `info` is a class. I recommend brushing up on the syntax sections of your text book. However if you find code like that in your textbook, [get a better book.](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Sep 25 '17 at 18:52

1 Answers1

1

You cannot reseat a reference. Once it's set, it's stuck there and any attempts to reassign the reference will be interpreted as a request to assign to the referenced variable. This means

person = staff[i];

is actually copying staff[i]; into person which is an alias (another name) for student[i]. student[i] will continue to receive the inputs read from the user.

The easiest way around this given your current code is to replace the reference with a pointer, which can be reseated.

class info* person = &student[i]; // using pointer

int age ;
cout<< "enter age"<<endl;
cin >> age;

if( age > 18 )
{
    person = &staff[i];  // using pointer, but note: nasty bug still here
                         // You may have empty slots in staff 
}

cout<< "enter unique_id"<<endl;
cin >> person->unique_id; // note the change from . to ->
....

But There are ways around this. You can delay creating the reference until you know which array to use. This requires shuffling around a lot of your code and would still leave you with unused elements in the array if you are not careful.

Fortunately there is a much better way to do this using std::vector from the C++ Standard Library's container library.

std::vector<info> student;
std::vector<info> staff;

for (int i=0; i<10;i++)
{
    info person; // not a pointer. Not a reference. Just a silly old Automatic

    int age ;
    cout<< "enter age"<<endl;
    cin >> age;

    // gather all of the information in our Automatic variable
    cout<< "enter unique_id"<<endl;
    cin >> person.unique_id;
    cout<< "enter height"<<endl;
    cin >> person.height;
    cout<< "enter weight"<<endl;
    cin >> person.weight;

    // place the person in the correct vector
    if( age > 18 )
    {
        staff.push_back(person);
    }
    else
    {
        student.push_back(person);
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54