Changing char statuses.

closed account (zbX9z8AR)
Hello, to explain my problem, I shall use an example. Say the user enters 3 islands with populations of only 5, 4, and 3 people. They put in 2 for the total infected. I would like for the code to display the first two people in each island with an 'i' for their status while the rest stay 'h' as in healthy.

Right now, this code displays
h 1
h 1
h 1
h 1
h 1
h 2
h 2
h 2
h 2
h 3
h 3
h 3

The first two people in each island should be changed to 'i'. So it should look like this.

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

Any help is appreciated.

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#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;
}

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 every_healthy, every_infected, every_infectious, every_recovered;
	int count = 0;
	char infectedstatus = 'i';
	while(every_infected != 0 && every_infectious != 0)
	{
		while(temp_person != NULL)
		{
			cout << temp_person->island << " " << temp_person->status << endl;
			temp_person = temp_person->next;
		}
	}
}

int main()
{
	int num, first_infected, contact_rate, transmission_probability, infected_period, infectious_period;
	int first, second, third;
	Island island_catalog;
	char healthstatus = 'h';
	cout << "How many islands would you like?\n";
	while((cin >> num).fail() || num < 1 || cin.peek() != '\n')
	{
		cin.clear();
		while(cin.get() != '\n')
		{
			continue;
			cin >> num;
		}
		cout << "ERROR: Please enter a non-zero, positive integer for the amount of islands.\n";
	}
	for(int i=0; i<num; i++)
	{
		int pop, island;
		cout << "Island "<< i+1 << " Population: ";
		cin >> pop;
		island = i+1;
		for(int j=0; j<pop; j++)
		{
			island_catalog.addPerson(healthstatus, island);
		}
		pop = 0;
		island = 0;
	}
	int transit;
	cout << "How many transit routes would you like?\n";
	while((cin >> transit).fail() || transit < -1 || cin.peek() != '\n')
	{
		cin.clear();
		while(cin.get() != '\n')
		{
			continue;
			cin >> transit;
		}
		cout << "ERROR: Please enter a non-zero, positive integer for the amount of transit routes you would like.\n";
	}
	cout << "For the transit routes, please enter first the island you want to take people from.\nFor the second number, please enter the island you want to take these people to.\nFor the third number, type in how many people you want to take from the first island to the second.\n";
	cout << "Transit Routes: ";
	for(int k=0; k<transit; k++)
	{
		first = 0;
		second = 0;
		third = 0;
		while((cin >> first).fail() || first < 1 || first > num || cin.peek() != '\n')
		{
			cin.clear();
			while(cin.get() != '\n')
			{
				continue;
				cin >> first;
			}
			cout << "ERROR: Please enter a non-zero, positive integer that is within the amount of islands you initially entered.\n";
		}
		cout << " -> ";
		while((cin >> second).fail() || second < 1 || second > num || second == first || cin.peek() != '\n')
		{
			cin.clear();
			while(cin.get() != '\n')
			{
				continue;
				cin >> second;
			}
			cout << "ERROR: Please enter a non-zero, positive integer that is within the amount of islands you initially entered or not the same island as your first number.\n";
		}
		cout << " ";
		while((cin >> third).fail() || third < 1 || cin.peek() != '\n')
		{
			cin.clear();
			while(cin.get() != '\n')
			{
				continue;
				cin >> third;
			}
			cout << "ERROR: Please enter a non-zero, positive integer that is within the population you asked for this island.\n";
		}
		cout << " people\n";
	}
	cout << "Total Infected = ";
	while((cin >> first_infected).fail() || first_infected < 1 || cin.peek() != '\n')
	{
		cin.clear();
		while(cin.get() != '\n')
		{
			continue;
			cin >> first_infected;
			cout << endl;
		}
		cout << "ERROR: Please enter a non-zero, positive integer that is within the population you asked for.\n";
	}
	cout << "Contact Rate = ";
	while((cin >> contact_rate).fail() || contact_rate < 1 || cin.peek() != '\n')
	{
		cin.clear();
		while(cin.get() != '\n')
		{
			continue;
			cin >> contact_rate;
			cout << endl;
		}
		cout << "ERROR: Please enter a non-zero, positive integer that is within the population you asked for.\n";
	}
	cout << "Transmission Probability = ";
	while((cin >> transmission_probability).fail() || transmission_probability < 1 || transmission_probability > 100 || cin.peek() != '\n')
	{
		cin.clear();
		while(cin.get() != '\n')
		{
			continue;
			cin >> transmission_probability;
		}
		cout << "ERROR: Please enter a non-zero, positive integer that is within 1 to 100%.\n";
	}
	cout << "%\n";
	cout << "Infected Period = ";
	while((cin >> infected_period).fail() || infected_period < 1 || cin.peek() != '\n')
	{
		cin.clear();
		while(cin.get() != '\n')
		{
			continue;
			cin >> infected_period;
		}
		cout << "ERROR: Please enter a non-zero, positive integer that is within 1 to 100%.\n";
	}
	cout << " days\n";
	cout << "Infectious Period = ";
	while((cin >> infectious_period).fail() || infectious_period < 1 || cin.peek() != '\n')
	{
		cin.clear();
		while(cin.get() != '\n')
		{
			continue;
			cin >> infectious_period;
		}
		cout << "ERROR: Please enter a non-zero, positive integer that is within 1 to 100%.\n";
	}
	cout << " days\n";
	island_catalog.work(first_infected, contact_rate, transmission_probability, infected_period, infectious_period, num);
	return 0;
}
> #include <vector>
use it, or std::list if you prefer
not going to debug yet another not invented here implimentation

> Right now, this code displays
> h 1
funny, I've got
1 h
having to guess a lot of input.

> 2 for the total infected. (...) the first two people in each island
so total doesn't mean total


about your code, 239 lines and not a single comment. ¿what I'm suppossed to look at?

> Island island_catalog;
wait, ¿what does an Island represent?
because it seems that you treat Island as a collection of islands.
Topic archived. No new replies allowed.