Losing elements when pushing to stack

The issue I am having is not all elements are pushing to my final list from my stacks. Hopefully the code and explanations will give enough detail. If there are any questions feel free to ask. Any help would be really appreciated.

Global function that takes my Male stack and Female Stack. It pushes them alternatively to my LHS (left hand side of a "table") and RHS (right hand side of a "table".
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
void BuildFinal(stack<Salesman, list<Salesman>> & MS, stack<Salesman, list<Salesman>> & FS)
{ 
	while (!MS.empty() && !FS.empty())
	{
		if (LHS.empty() || (LHS.top().getGender() == female) && !MS.empty())
		{
			LHS.push(MS.top());
			MS.pop();
		}
		else if ((LHS.top().getGender() == male) && !FS.empty())
		{
			LHS.push(FS.top());
			FS.pop();
		}
		else if (((LHS.top().getGender() == female) && FS.empty()) || ((LHS.top().getGender() == male) && FS.empty()))
		{
			LHS.push(MS.top());
			MS.pop();
		}
		else if (((LHS.top().getGender() == male) && MS.empty()) || ((LHS.top().getGender() == female) && MS.empty()))
		{
			LHS.push(FS.top());
			FS.pop();
		}

		if ((RHS.empty()) || (RHS.top().getGender() == male) && !FS.empty())
		{
			RHS.push(FS.top());
			FS.pop();
		}
		else if ((RHS.top().getGender() == female) && !MS.empty())
		{
			RHS.push(MS.top());
			MS.pop();
		}
		else if (((RHS.top().getGender() == female) && FS.empty()) || ((RHS.top().getGender() == male) && FS.empty()))
		{
			RHS.push(MS.top());
			MS.pop();
		}
		else if (((RHS.top().getGender() == male) && MS.empty()) || ((RHS.top().getGender() == female) && MS.empty()))
		{
			RHS.push(FS.top());
			FS.pop();
		}
	}

}


This is in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
BuildFinal(MaleStack, FemaleStack);//Call function above. With the below //functions I push from my LHS and RHS into the final seating list.
	
	while (!RHS.empty())
	{
		RHSsorted.push(RHS.top());
		RHS.pop();
	}

	while (!LHS.empty())
	{
		FinalSeating.push_back(LHS.top());
		LHS.pop();
	}

	FinalSeating.push_back(Salesman("Guest Speaker", 0, male));

	while (!RHSsorted.empty())
	{
		FinalSeating.push_back(RHSsorted.top());
		RHSsorted.pop();
	}

Here is an example of some of the output, but it does this as well even if the original list has more Lady Salesman. Also both male and females are generated randomly.

Sorting
Salesman Name: Lady Salesman Sex: Female Sales Amount: $133

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $305

Salesman Name: Lady Salesman Sex: Female Sales Amount: $395

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $423

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $428

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $566

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $626

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $644

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $695

Done Sorting
Press any key to continue . . .
Sepearating Lists
Done Seperating Lists
Bulding Final List
Printing Final List

Press any key to continue . . .
Salesman Name: Lady Salesman Sex: Female Sales Amount: $133

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $695

Salesman Name: Guest Speaker Sex: Male Sales Amount: $0

Salesman Name: Lady Salesman Sex: Female Sales Amount: $395

Salesman Name: Gentleman Salesman Sex: Male Sales Amount: $644

Press any key to continue . . .
Last edited on
Please, edit your post to contain code tags (and reasonable indentation) http://www.cplusplus.com/articles/jEywvCM9/
Alright code should be a bit easier to understand. Any help would be awesome.
The while will run only as long as both MS and FS are not empty. Once either becomes empty, the loop will exit. I suspect you want it to run as long as either stack has elements.

Also, see if you can make your conditionals simpler. It's difficult to tell what the code is supposed to be doing.
Yeah as I looked through the code I thought the initial condition for the while loop was causing that. But I can't seem to think of what I can do for the condition. For the rest of the code, I am pushing the gentleman and female salesman alternatively to the LHS and RHS. Afterwards pushing both the LHS and RHS to the final list as well as a "Guest Speaker". Viewing the table would be seen as Salesman with the largest sales amount next to the Guest speaker. But it starts with the a gentleman to the left and a lady to the right. And from there is alternates. I hope that helps any extra input or example would be great.
Look at http://www.cplusplus.com/reference/algorithm/merge/
That uses both until one runs out. Then it dumps the remaining.

Rather than using many ifs, consider this:
1
2
3
4
5
6
7
bool side = true;
while (...) {
  if ( side ) // add left
  else // add right

  side = !side;
}

The side for next item is computed. Same can be used for gender.
Last edited on
Alright I'll check into the link, but I am little confused of how to implement what you are showing.
Topic archived. No new replies allowed.