Objects

Logic:
Input 5 countries
by name
by population
by area
Output:
Name of country with highest Population and its number
Name of country with largest Area and its name
Name of country with the density of population (population/area)

Having trouble running the switch in the class and also showing output. Any ideas?

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
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class country
{
public:
	country();
	country (string n, long p, int s);

	void set_name(string in_n);
	void set_pop(long in_p);
	void set_area(double in_a);
	void raise_price(double in_inc);

	string get_name() const;
	long get_pop() const;
	int get_area() const;
	double get_density() const;

	bool is_better_than(country b) const;

private:
	string country_name;
	long country_pop;
	int country_area;

};

country::country()
{
	country_name = "NONE YET";
	country_pop = 0.01;
	country_area= 0;
}
country::country(string in_name, long in_pop, double in_area)
{
	country_name = in_name;
	country_pop = in_pop;
	country_area = in_area;

}

void country::set_name(string in_name)
{
	country_name = in_name ;
}
void country::set_pop(long in_pop)
{
	country_pop = in_pop ;
}
void country::set_area(double in_area)
{
	country_area = in_area ;
}
string country::get_name() const
{
	return (country_name);
}
long country::get_pop() const
{
	return (country_pop);
}
int country::get_area() const
{
	return (country_area);
}
double country::get_density() const
{
	double value = 0.0; 
	if (country_pop > 0)
		value = country_pop / country_area;
	if (country_pop <= 0)
		value = 9999999.9;
	return value;

	bool country::is_better_than(country b) const
	{
		bool iam_better = true;
		int situation = 0;
		double in_b_pop = 0.0;
		in_b_pop = b.get_pop();
		if ( (in_b_pop == 0) && (country_pop > 0) )
			situation = 1;
		if ((country_pop == 0) && (in_b_pop > 0))
			situation = 2;
		if ((country_pop == 0) && (in_b_pop == 0))
			situation = 3;
		if ( (country_pop > 0) && (in_b_pop > 0))
			situation = 4;

		switch (situation)
		{
		case 1: iam_better = false;
			break;
		case 2: iam_better = true;
			break;
		case 3: iam_better = true; 
			break;

			if ( get_density() > b.get_density() )
				iam_better = true;
			else iam_better = false;
			break;
		default:
			iam_better = false;
			break;
		}
		return iam_better;
	} 

}

int main ()
{

	string c_name = "";
	long c_pop = 0.0;
	int c_area = 0.0;
	int winner_area=0.0;
	long winner_pop=0.0;
	int winner_density=0.0;
	bool more=true;
	country top_country;
	//to hold info of the top country , the incumbent
	cout << "About the newly created product object for the dummy incumbent: " <<
		endl;
	cout << "Default Name given by constructor: " <<
		top_country.get_name() << endl;
	cout << fixed << setprecision(2);
	cout << "Default Price given by constructor: " <<
		top_country.get_pop() << endl;
	cout << "Default Score given by constructor: " <<
		top_country.get_area() << endl;
	cout << "Calculated Value Index of Incumbent: " <<
		top_country.get_density() << endl;
	cout << "We can set up this dummy incumbent because it is not possible "
		<< "to get a negative " << "price and " ;
	cout << "a negative score." << endl;
	cout << " " << endl;



	cout << "Please enter the first country:";
	cin >> c_name;
	cin.ignore(256, '\n');
	cout << "Please enter the population of this country in a whole figure:";
	cin >> c_pop;
	cout << "Please enter the area of this country in square kilometers: ";
	cin >> c_area;
	cout << "  "<< endl;

	country c1(c_name, c_pop, c_area);

	cout << "Please enter the second country:";
	cin >> c_name;
	cin.ignore(256, '\n');
	cout << "Please enter the population of this country in a whole figure:";
	cin >> c_pop;
	cout << "Please enter the area of this country in square kilometers: ";
	cin >> c_area;
	cout << "  "<< endl;
	country c2(c_name, c_pop, c_area);
	c2.set_name(c_name);
	c2.set_pop(c_pop);
	c2.set_area(c_area);
	cout<< "  "<< endl;

	cout << "Please enter the third country:";
	cin >> c_name;
	cin.ignore(256, '\n');
	cout << "Please enter the population of this country in a whole figure:";
	cin >> c_pop;
	cout << "Please enter the area of this country in square kilometers: ";
	cin >> c_area;
	cout << "  "<< endl;
	country c3(c_name, c_pop, c_area);
	c3.set_name(c_name);
	c3.set_pop(c_pop);
	c3.set_area(c_area);
	cout<< "  "<< endl;

	cout << "Please enter the fourth country:";
	cin >> c_name;
	cin.ignore(256, '\n');
	cout << "Please enter the population of this country in a whole figure:";
	cin >> c_pop;
	cout << "Please enter the area of this country in square kilometers: ";
	cin >> c_area;
	cout << "  "<< endl;
	country c4(c_name, c_pop, c_area);
	c4.set_name(c_name);
	c4.set_pop(c_pop);
	c4.set_area(c_area);
	cout<< "  "<< endl;

	cout << "Please enter the fifth country:";
	cin >> c_name;
	cin.ignore(256, '\n');
	cout << "Please enter the population of this country in a whole figure:";
	cin >> c_pop;
	cout << "Please enter the area of this country in square kilometers: ";
	cin >> c_area;
	cout << "  "<< endl;
	country c5(c_name, c_pop, c_area);
	c5.set_name(c_name);
	c5.set_pop(c_pop);
	c5.set_area(c_area);
	cout<< "  "<< endl;


	cout << "Final Winner: " << endl;
	cout << "Country with the largest population:" << top_country.get_name()
		<<top_country.get_pop()<<endl;
	cout << "Country with the largest area:" << top_country.get_name()
		<<top_country.get_area<< endl;
	cout << "Country with the highest population density: "<< top_country.get_name()
		<<top_country.get_density()<< endl;
	cout << " " << endl;
	cout << "Bye!" << endl;
	return 0;

} 









1. Try writing a function for getting country information which returns a country object. (you are doing the same thing again and again(5 times) within main()).
2. Store the objects within a country array. Try it .
3. Sort the array. a) on the basis of pop b.) area c.) density.

By the way , have you intentionally written the country::isbetterthan(country b) inside the country::get_density() function ?

Try to keep main least crowded so that you don't write redundant code. It will make your life easier.

Peace.

Topic archived. No new replies allowed.