0

In my program, I have a vector of structs, and I'm trying to iterate through the vector while accessing the properties of each struct. However, the way I'm trying to do this doesn't seem to work. The struct in question is called "person", and it has properties including "Name" and "PartyID", which are the ones I'm trying to access.

I read somewhere online you could do this using pointers to the iterator, so that's what I tried to do (*it.Name and *it.PartyID), but the program won't compile. What am I doing wrong? Here are the errors I'm getting (using G++ to compile the program):

Errors

And here is the code I've written:

// DelibDem.cpp : Defines the entry point for the application.
//

#include "DelibDem.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <vector>

//initializing variables
using namespace std;
bool continue_ = true;
string name = "";
string partyID = "";
int numD = 0;
int numR = 0;
int difference = 0;
int vectorSize = 0;
int newVectorSize = 0;
int vectorPos = 0;
int demPos = 0;
int repPos = 0;
struct person{
    string Name;
    string PartyID;
    string equivalentName;
    string equivalenceClass;
};
vector<person> Sample;

int main()
{
    //user adds people to the vector
    while (continue_ == true) {
        string personName;
        string personPartyID;
        string answer;
        person inputtedPerson;
        cout << "Enter a person's name: ";
        std::getline(cin, personName);
        //cout << personName;
        cout << "Enter the person's party ID (D or R): ";
        std::getline(cin, personPartyID);
        //cout << personPartyID;
        if (personPartyID == "D") person inputtedPerson = { personName, personPartyID, "", "Republicans" };
        else person inputtedPerson = { personName, personPartyID, "", "Democrats" };
        Sample.at(vectorPos) = inputtedPerson;
        //cout << "{{" << Sample.at(0).Name << ", " << Sample.at(0).PartyID << ", " << Sample.at(0).equivalentName << ", " << Sample.at(0).equivalenceClass << "}\n";
        //for (int i = 1; i < Sample.size(); i++) {
        //  cout << ", {" << Sample.at(i).Name << ", " << Sample.at(i).PartyID << ", " << Sample.at(i).equivalentName << ", " << Sample.at(i).equivalenceClass << "}\n";
        //}
        //cout << "}";
        vectorPos++;
        cout << "Do you wish to add more people? (Y/N) ";
        cin >> answer;
        if (answer == "N") continue_ = false;
        cin.ignore();
    }
    //The number of Democrats in the sample is stored in numD. The number of Republicans is stored in numR.
    for (auto& element : Sample)
    {
        if (element.PartyID == "D") numD++;
        else numR++;
    }
    //print the number of Democrats and Republicans
    cout << numD;
    cout << numR;
    //determine if an equivalence relation exists
    if (numD == numR) cout << "An equivalence relation is possible" << endl;
    else {
        cout << "An equivalence relation is not possible, because ";
        if (numD > numR) {
            cout << "There are " << numD - numR << " more Democrats than Republicans" << endl;
            int difference = numD - numR;
            while (difference != 0) {
                string specifiedName;
                vectorSize = Sample.size();
                cout << "Which Democrat do you want to remove from the sample?" << endl;
                cin >> specifiedName;
                for (std::vector<person>::iterator it = Sample.begin(); it != Sample.end(); ++it) //this is the part I'm asking about
                {
                    if (*it.Name == specifiedName && *it.PartyID == "D") {
                        Sample.erase(it);
                        break;
                    }
                }
                newVectorSize = Sample.size();
                if (vectorSize == newVectorSize) cout << "The requested person is not one of the Democrats in the sample. Please try again." << endl;
                else difference--;
            }
        }
        else {
            cout << "There are " << numR - numD << " more Republicans than Democrats" << endl;
            int difference = numR - numD;
            while (difference != 0) {
                string specifiedName;
                vectorSize = Sample.size();
                cout << "Which Republican do you want to remove from the sample?" << endl;
                cin >> specifiedName;
                for (std::vector<person>::iterator it = Sample.begin(); it != Sample.end(); ++it)
                {
                    if (*it.Name == specifiedName && *it.PartyID == "R") {
                        Sample.erase(it);
                        break;
                    }
                }
                newVectorSize = Sample.size();
                if (vectorSize == newVectorSize) cout << "The requested person is not one of the Republicans in the sample. Please try again." << endl;
                else difference--;
            }

        }
        cout << "The number of Democrats and Republicans is now equal and an equivalence relation is possible." << endl;
    }
    //print the properties of each element in the sample
    for (auto& element : Sample)
    {
        cout << element.Name << endl;
        cout << element.PartyID << endl;
        cout << element.equivalentName << endl;
        cout << element.equivalenceClass << endl;
        cout << "\n";
    }
    //creating two vectors containing only the Democrats and Republicans, respectively
    vector<person> Democrats;
    vector<person> Republicans;
    for (auto& element : Sample)
    {
        if (element.PartyID == "D") {
            Democrats.at(demPos) = element;
            demPos++;
        }
        else {
            Republicans.at(repPos) = element;
            repPos++;
        }
    }
    //assigning each Democrat to a Republican and vice versa
    for (int i = 0; i < Democrats.size(); i++)
    {
        Democrats.at(i).equivalentName = Republicans.at(i).Name;
    }
    for (int i = 0; i < Republicans.size(); i++)
    {
        Republicans.at(i).equivalentName = Democrats.at(i).Name;
    }
    return 0;
    //printing the properties of each element in the sample again
    for (auto& element : Sample)
    {
        cout << element.Name << endl;
        cout << element.PartyID << endl;
        cout << element.equivalentName << endl;
        cout << element.equivalenceClass << endl;
        cout << "\n";
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 2
    Use either `(*it).PartyID` or `it->PartyID`. `*it.PartyID` is the same as `*(it.PartyID)`. – Some programmer dude Dec 03 '22 at 04:20
  • The rules of [operator precedence](https://en.cppreference.com/w/cpp/language/operator_precedence) are that `.` has higher precedence than `*`. Therefore `*it.Name` is equivalent to `*(it.Name)`, not `(*it).Name`. As mentioned above, you can also use `it->Name` to dereference and access a member. – Nathan Pierson Dec 03 '22 at 04:21
  • 1
    Also be careful with [erasing while iterating](https://stackoverflow.com/questions/596162/can-you-remove-elements-from-a-stdlist-while-iterating-through-it). You might want to look into `erase_if` – Nathan Pierson Dec 03 '22 at 04:23
  • Thanks Some programmer dude and Nathan Pierson! Your suggestions fixed my problem. – StanAtkinson Dec 03 '22 at 04:32

0 Answers0