jeudi 28 mars 2019

For loop doesn't print all the details of the second vector element

I'm creating a database for a University project, I've come across an issue, the it prints the first element and the associating details for that agent, but for the second agent, only their name and status are printed, not their location or salary.

I've shortened my code to just display the agent name and location only the first element prints as expected, the second element does not (only prints the name.

Any advice would be greatly appreciated, thank you.

I've tried stepping through the code but nothing stands out, I'm only three months into C++

Main

string name;
    string status;
    int baseSalary = 0;
    vector<agent> members;
    location destination;


     for (int i = 0; i < maxAgents; i++)
     {
    cout << "Enter agent name: " << endl;
        cin >> name;
        cout << "Enter agent status: " << endl;
        cin >> status;
        members.push_back({ name, status, baseSalary });
    }


    for (int j = 0; j < maxAgents; j++)
    {
        if (members[j].getAgentStatus() == "Active")
        {
            destination.changeLocation(members, maxAgents);
cout << "\nAgent Name: " << members[j].getAgentName();
            cout << "\nAgent location: " << members[j].getAgentLocation();
        }
    }

location.cpp

agent location::changeLocation(vector<agent> &member, int size)
{
    srand((unsigned int)time(NULL));

    for (int i = 0; i < size; i++)
    {
        if (member[i].getAgentStatus() == "Active")
        {
            int location = rand() % 5 + 1;
            switch (location)
            {
            case 1:
                member[i].setAgentLocation("Russia");
                member[i].setSalary(45000);
                break;
            case 2:
                member[i].setAgentLocation("Middle-East");
                member[i].setSalary(60000);
                break;
            case 3:
                member[i].setAgentLocation("Turkey");
                member[i].setSalary(28000);
                break;
            case 4:
                member[i].setAgentLocation("USA");
                member[i].setSalary(40000);
                break;
            case 5:
                member[i].setAgentLocation("North Korea");
                member[i].setSalary(70000);
                break;
            }
        }
        return member[i];
    }
}

Agent name: input

Agent Location: Randomly generated via destination.changeLocation(members, maxAgents).

Aucun commentaire:

Enregistrer un commentaire