Exiting a loop too soon

Information: the function is supposed to print out multiple "sets" of users. Think of it as separate groups of friends, all interconnected within each group, but nobody in any group is connected to anybody outside their group.

My problem: the function only prints the first set (all users connected to user 0, since that's where I manually start the loop). There are supposed to be four sets.

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
void list_sets()
{
	if (! Users.size()) cout << "\tNo users in database." << endl;
	else {
		deque<int> queue;
		int numsets = 0;
		int current;
		unsigned int numchecked = 1;

		while (numchecked < Users.size()) { // Users is a vector of 44 elements
			numsets++;
			queue.clear();
			int j = 0;
			while (! queue.size()) {
				if (! Users[j].checked) queue.push_back(j);
				else j++;
			}
			current = queue[0];
			Users[current].checked = true;
			cout << "\nSet " << numsets << " => " << Users[current].username;
			while (queue.size()) {
				for (unsigned int i = 0; i < Users[current].friends.size(); i++) {
					if (! Users[Users[current].friends[i]].checked) {
						cout << ", ";
						
						cout << Users[Users[current].friends[i]].username;
						queue.push_back(Users[current].friends[i]);
						Users[Users[current].friends[i]].checked = true;
						numchecked++;
					}
					if (numchecked == Users.size()) break;
				}
				queue.pop_front(); 
				if (queue.size()) current = queue[0]; Users[current].checked = true;
				if (numchecked == Users.size()) break;
			}
			if (numchecked == Users.size()) cout << endl; break;
		}
	}
}


I've done tons of error-checking by inserting cout tests and I can't find out why the while (numchecked < Users.size()) loop won't iterate even a second time, let alone a fourth time. Any help will be appreciated.

EDIT: Forgot to specify that Users is a vector of User objects, which contain a bool member called checked, which just determines whether or not that User has been put in a "set" yet.
Last edited on
Without full code and input data, I can't run it, so I'll guess;

numchecked++; is in the wrong location would be my first guess.

should

if (queue.size()) current = queue[0]; Users[current].checked = true;

be

1
2
3
4
5
if (queue.size())
{
     current = queue[0];
     Users[current].checked = true;
}


so that both lines get executed if the queue is not 0 length?
Topic archived. No new replies allowed.