Vector issue / Classes

I am having a weird issue with vectors. I can put items into the vector. But when I got to look inside the vector it's empty. See code below.

Sean

int main
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
int main()
{
	// Classes and variables
	EmpRecord *newRecord = new EmpRecord();
	Console *console = new Console;
	vector<Employee> listVector;
	string choice = "";

	// Load records (automatic)
	newRecord->loadVector( listVector );

	// Menu loop
	while( choice != "exit" )
	{
		cout << "Console: ";
		cin >> choice;
		processChoice( console->checkCommand( choice ) );			// Run the command
	}


	// Save (upon exit)

	cin.ignore();
	cin.get();

	return 0;
}


// function under main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void processChoice( string &a )
{

	EmpRecord *newRecord = new EmpRecord;
	Employee *employee = new Employee();
	Console *console = new Console;
	vector<Employee> listVector;

	while( a != "end" )
	{
		if( a == "list" ) { newRecord->displayRecord( listVector ); break; }
		if( a == "help" ) { console->listCommands(); break; }
		if( a == "search" ) { break; }
		if( a == "add" ) { newRecord->addToVector( listVector ); break; }
		if( a == "exit" ) { cout << "\nExiting!!\n\n"; break; }
		else { cout << "\nInvalid choice.\n\n"; break; }
	}
}


// code to add to the vector
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
void EmpRecord::addToVector( vector<Employee> newlistVector )
{
	// Temp variables
	int id = 0, pay = 0, count = 0;
	float tax = 0.0;
	string name1, name2;

	// Adding a record to the Array
	cout << "\nNew employee record.\n"
		 << "Please enter the following information as prompted.\n\n";

	cout << "How many records will you be adding? ";
	cin >> count;

	for( int i = 0; i < count; i++ )
	{
		id = ( newlistVector.size() + 1 );
		cout << "\nEmployee first name: ";
		cin >> name1;

		cout << "\nEmployee last name: ";
		cin >> name2;

		cout << "\nEmployee hourly wage: ";
		cin >> pay;

		cout << "\nEmployee tax rate is 32%";
		tax = 0.32;
		cout << "\n\n";

		// Add the record to the vector
		
		newlistVector.push_back( Employee( id, name1, name2, pay, tax ) );
	}
}

this code works fine. I can see it fill out the vector in the debugger. However when I run the next command "list" it says the vector is empty

// Display vector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void EmpRecord::displayRecord( const vector<Employee> newlistVector )
{
	int size = newlistVector.size();

	// Check to see if Employee Array is empty
	if( lastID <= 1 ) { cout << "\nNo records in Database\n\n"; }

	else
	{
		// List the records on the screen
		for( int i = 0; i < size; i++ )
		{
			cout << newlistVector[i].getID() << " "
				 << newlistVector[i].getFirst() << " "
				 << newlistVector[i].getLast() << " "
				 << newlistVector[i].getPay() << " "
				 << newlistVector[i].getTax();
		}
		cout << "\n\n";
	}	
}


Any help would be great. I've managed to stump myself.
Your addToVector method should pass a reference to the vector passed instead of a copy.

So change:

void EmpRecord::addToVector( vector<Employee> newlistVector )


to

void EmpRecord::addToVector( vector<Employee> & newlistVector )

Also try changing your displayRecord method to pass a const reference parameter - this should avoid the method receiving its own copy thereby making it inefficient.
In main, you have a vector listVector that you feed to loadVector.

In processChoice you have a vector listVector. Despite the fact that the vector in processChoice shares a name with the vector in main they are entirely separate and distinct objects. One has absolutely nothing to do with the other, so in processChoice you will not have access to the vector that you loaded with loadVector.

addToVector takes a vector by value as a parameter. This means that in processChoice a copy of the local vector listVector is fed to addToVector. This means that no changes made (to the copy) in addToVector will be reflected in the local-to-processChoice vector that was fed to the function. addToVector should take its parameter by reference:
void EmpRecord::addToVector( vector<Employee>& newlistVector )

Likewise, displayRecord need not make a copy of the vector. Passing by const reference would be preferred.
does the placement of the & make a difference? You have it

void EmpRecord::addToVector( vector<Employee>& newlistVector ) <-- here

and the other guy did
 
void EmpRecord::addToVector( vector<Employee> & newlistVector )
<-- this
I am passing the display by constant

void EmpRecord::displayRecord( const vector<Employee> newlistVector )
Changed...
void EmpRecord::addToVector( vector<Employee>& newlistVector )

changed....
void EmpRecord::loadVector( vector<Employee>& newlistVector )

changed...
void EmpRecord::displayRecord( const vector<Employee> newlistVector )
does the placement of the & make a difference? You have it

No. It's a matter of style.

I am passing the display by constant

void EmpRecord::displayRecord( const vector<Employee> newlistVector )


You are passing by value. All the const means is that you promise not to modify the freshly made copy in the body of the function.

void EmpRecord::displayRecord(const vector<Employee>& newListVector) const

Your code has other design issues. Why is an employee record responsible for displaying an unrelated list of employee records?
Employee is the base class. when I try to EmpRecord I get errors.

yea, that did not fix it. The vector is still empty. I maybe have to rework a few things.
Last edited on
I fixed this issue. I just re-wrote the entire program from scratch. I redid my design a bit. Everything seems to working much better now. Thank you all for your help and comments. :-)

Sean
Topic archived. No new replies allowed.