#include <iostream>
#include <vector>
#include <string>
#include <map>
usingnamespace std;
struct bop {
string realname; //real name
string title; //job title
string bopname; //secret Benevolent Order of Programmers name
int preference; // 0 = fullname, 1 = title, 2 = bopname
};
void enterInformation(vector<bop> employer, int programmers);
void informationBreach(vector<bop> employer, int programmers);
int main()
{
//# of secret BOP programmers
int programmers = 0;
cout << "How many programmers: ";
cin >> programmers;
cin.get(); //clears \n
vector<bop> employer(programmers);
//Enters BOP info
enterInformation(&employer, programmers);
informationBreach(employer, programmers);
}
void enterInformation(vector<bop> *employer, int numProgrammers)
{
for (int i = 0; i < numProgrammers; i++)
{
cout << "///NOTE: ALL INFORMATION IS SECRET AND WILL ONLY DISPLAY YOUR PREFERENCE\\\\\\" << endl;
cout << "Real name: ";
getline(cin, *employer[i]->realname);
cout << "Title: ";
getline(cin, *employer[i]->title);
cout << "BOP Name: ";
getline(cin, *employer[i]->bopname);
cout << "Preference[0 = Full Name : 1 = Title Name : 2 = BOP Name]: ";
while (!((cin >> *employer[i]->preference) && *employer[i]->preference >= 0 && *employer[i]->preference <= 2))
{
cout << "Preference must be 0-2." << endl;
while (cin.get() != '\n') //removes bad input
cin.clear(); //allows new input
}
cin.get(); //reads \n from the while loop
}
}
void informationBreach(vector<bop> employer, int numProgrammers)
{
for (int i = 0; i < numProgrammers; i++)
{
cout << "\a\a//////////INFORMATION BREACH 100% SUCCESSFUL//////////\a\a\n";
cout << "Real Name: " << employer[i].realname << endl;
cout << "Title: " << employer[i].title << endl;
cout << "BOP Name: " << employer[i].bopname << endl;
cout << "Preference: " << employer[i].preference << endl;
}
}
Okay, so first thing's first. The program will not compile due to lines 39-45.
If I were to change those pointers into regular objects, it will not change the values of my class object. So what is the right way to do this?
I want the user to be able to input the # of employers/programmers into the system. But I cannot do that with an array of classes because when declaring an array; the array size must be constant.
I'd use a reference to a vector instead of a pointer, but it is possible either way. With a pointer, to access the data you'll need syntax like: (*employer)[i].realname
Instead of *employer[i]->realname, make sure you are using employer[i]->realname this is because in the first piece of code you are dereferencing employer, and then dereferencing it again by using the -> operator. Remember, the -> operator takes the instance at the location of the pointer and finds the instance of it.
Edit: If you still get the same error you that you got in your last post, replace the -> with a dot.
Never mind, I thought about this for a few seconds and realized that this won't work.
when you use pointers in lines 39-45, you have to change all of your codes to use pointers
if you dont change them, you will get this errors
or you can convert variables to pointers