[Frustration] Pointer Class Object Vectors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace 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.
Last edited on
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
How would you use a reference to a vector?

Okay, and now I changed lines 39-45 to (*employer)[i], but now another compile error appears on line 27.

1
2
3
Error	1	error C2664: 'enterInformation' : cannot convert parameter 1 from
 'std::vector<_Ty> *' to 'std::vector<_Ty>'	c:\documents\visual studio 
2012\projects\ch6e4.2\ch6e4.2\source.cpp	27	1	ch6e4.2
Last edited on
Your prototype needs to say the function takes a pointer to a vector, as your implementation does.
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.
Last edited on
better go ahead with static vectors instead of pass by reference!!!
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
Topic archived. No new replies allowed.