Changing the CHAR status of the correct person.

closed account (zbX9z8AR)
Let's say the user wants the first 2 people infected from each island. Right now, this code prints out the first 5 people of the whole list.

If I choose 3 people for three islands, what I'm trying to output is...

i
1
i
1
h
1
i
2
i
2
h
2
i
3
i
3
h
3

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Person
{
friend class Island;

private:
	char status;
	int island;
public:
	Person *next;
	Person();
	Person(char&, int&);
	char setStatus(char&);
};

Person::Person()
{
}

Person::Person(char& s, int& i)
{
	status = s;
	island = i;
	next = NULL;
}

char Person::setStatus(char& s)
{
	status = s;
}

class Island
{
private:
	Person *root;
public:
	Island();
	~Island();
	void addPerson(char&, int&);
	void work(int&, int&, int&, int&, int&, int&);
};

Island::Island()
{
	root = new Person();
	root = NULL;
}

Island::~Island()
{
	delete root;
}

void Island::addPerson(char& healthstatus, int& island)
{
	Person *new_civilian = new Person(healthstatus, island);

	if(root == NULL)
	{
		root = new_civilian;
		return;
	}
	else
	{
		Person *temp_person = root;
		while(temp_person->next != NULL)
		{
			temp_person = temp_person->next;
		}
		temp_person->next = new_civilian;
	}
}

void Island::work(int& first_infected, int& contact_rate, int& transmission_probability, int& infected_period, int& infectious_period, int& num)
{
	Person *temp_person = root;
	int count = 0;
	char infectedstatus = 'i';
	while(every_infected != 0 && every_infectious != 0)
	{
		while(temp_person != NULL)
		{
			for(int l=0; l<num; l++)
			{
				for(int m=0; m<first_infected; m++)
				{
					if(temp_person->island == l+1)
					{
						temp_person->setStatus(infectedstatus);
					}
				}
				break;
			}
			cout << temp_person->status << endl;
			cout << temp_person->island << endl;
			temp_person = temp_person->next;
		}
	}
}
Lines 50-51: You have a memory leak here. Line 51 makes the Person that was created at line 50 inaccessible.

As for your problem, I would look at your loops from line 84-103. It's not clear where a number of your variables (every_infected, every_infectious) come from, so it's hard to comment.

To me it appears that you are looping through every person on an Island (line 86). The purpose of the for loop at line 90 is unclear since nothing in the for loop depends on m. The intent of the if statement at line 92 is unclear since temp_person is obtained from a single Island's linked list (root). The break statement at line 97 is going to exit out of the outer for loop on the first iteration, so why have the for loop?
Topic archived. No new replies allowed.