Past assignment

finished a C++ class a while back... I got a d+ :/

I'm workin on this now because I do want to learn but was a little too busy with life and things to focus. So I have a past assignment that I was never able to finish and get working correctly.

Define a class country as follows:
Class: country
Define a class called country which has the following private data fields:
1. Name of the country (use the string data type).
2. Population of the country (use the long data type).
3. Area of the country in square kilometers (use the double data type).

This class should have the following constructors:
1. A programmer-defined default constructor.
2. A programmer-defined constructor that accepts one incoming parameter value for each private field. These incoming parameter values are for initializing a newly created country object.

This class should have the following public methods:
1. A get method for each private data field of country.
2. A set method for each private data field of country.
3. A method for deriving the population density of country with the following formula:
population density per square kilometer (as a double value) = population / area

Program: main()
Write a program main() that asks a user for full information of an indefinite number of actual countries until the user chooses to quit. You can assume that the user will enter each piece of information in the correct data type.

For example:
Please enter the next country (-99 = no more):
Please enter the population of this country in a whole figure:
Please enter the area of this country in square kilometers:
You can assume that all country names keyed in by the user are just one-word names and the user will key in unique country names.


After the user has keyed in the full information of a country, program main() instantiates a corresponding country object for each one. It also stores each such object in a vector of these objects.

After the user has keyed in all the countries, program main() examines these objects and looks for winning countries in three different categories:

(1) having the largest population
(2) having the largest area
(3) having the highest population density.

They may be three different ones that
the user has keyed in.

For simplicity, if the corresponding figures are the same, for example, the population figure of country B is the same as the population figure of the current winner in this category, say, country A, then the incumbent
(A) is still the only winner in this category (having the highest population).

Then program main() displays the detailed information of each country one by one, looping through all objects in vector after user enters in sentinel. such as:

Countries entered are:
Country: USA
Population: 295734134
Area: 9631419 square kilometers
Population Density: 30.71 people per square kilometer

Country: Canada
Population: 32876200
Area: 9984670 square kilometers
Population Density: 3.29 people per square kilometer
…..

Then program main() displays the total number of countries the user has entered. For example:
Total number of countries entered: 12

Finally, program main() displays the winners in the three categories as calculated:
Country with the largest population: China - Population: 1321000000
Country with the largest area: Russia - Area: 16995800 square kilometers
Country with the highest population density: Macau - Density: 20824.4
people per square kilometer

Program main() then displays “Bye!” to finish execution.
I am getting many errors :/ some appear to be simple but I'm over looking it

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
#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 << "Top countries are: " << endl;
	cout << "Country Name: " << top_country.get_name() << endl;
	cout << fixed << setprecision(2);
	cout << "Country Population: " << top_country.get_pop() << endl;
	cout << "Country Area: " << top_country.get_area() << endl;
	cout << "Population Density: " << top_country.get_density() << 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;

}



first I have it set to view 5, I'm not sure how to get it to do an unlimited number of countries.

all highlighted areas are where im getting errors




Last edited on
Oh I also need to incorporate a sentinel of -99 :/

just totally lost. It doesn't help that my kids destroyed my book either. So my only references are online sources.
says im getting errors in these places:



**** Incremental Build of configuration Debug for project hw4 ****
Info: Internal Builder is used for build

g++ -O0 -g3 -Wall -c -fmessage-length=0 -o country.o "..\\country.cpp"



..\country.cpp:37:1: error: prototype for 'country::country(std::string, long int, double)' does not match any in class 'country'
country::country(string in_name, long in_pop, double in_area)
^
..\country.cpp:6:7: error: candidates are: country::country(const country&)
class country
^
..\country.cpp:10:2: error: country::country(std::string, long int, int)
country (string n, long p, int s);
^
..\country.cpp:31:1: error: country::country()
country::country()
^
..\country.cpp: In member function 'double country::get_density() const':
..\country.cpp:79:2: error: a function-definition is not allowed here before '{' token
{
^
..\country.cpp:216:1: error: expected '}' at end of input
}
^

15:35:25 Build Finished (took 391ms)

Could you edit your posts and put [code] tags around your code? That will preserve indentation and add syntax highlighting, line numbers, etc. which would make it easier for us to help.
You declared one of constructora in the class as

country (string n, long p, int s);

that is with the third parameter of type int but defined it as

country::country(string in_name, long in_pop, double in_area)

that is with the third parameter of type double.

And you forgot to place a closing brace at the end of function get_density

1
2
3
4
5
6
7
8
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;


Where is the closing brace?
Last edited on
I got it. Must have overlooked it :/

but what about adding infinite number of countries?
Topic archived. No new replies allowed.