Stumped on a practice question for hours someone please help!!!

I am trying to get better at c++ so I'm practicing with some problems. This one is very hard for me since I'm still a beginner. heres the question:
Define a class Country that stores the name of the country, its population, and its area. Store the country name as a c-string. Write a main function that initializes an array with at least 5 objects of this class and sorts and prints this array in three different ways:
• By population
• By population density (people per square kilometer)
• Alphabetically by name, ignoring the case (upper-case or lower-case)
Do not ask the user to enter country data. Use qsort to sort the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
#include <string>
using namespace std;


class Country {
  string name;
  long population;
  double area;
  
  Country(string name, long population, double area) {
    name; 
    population; 
    area; 
int main () {

  return 0;
}


that is my code so far. Im very lost, as you can see
closed account (E0p9LyTq)
There is a lot that is not right with that code snippet.

1. you are not finishing the class definition with a }; terminating brace.

2. your constructor is private access.

3. you are not initializing your class data members in your constructor.

4. your country name member variable is a C++ string, not a C string. Not such a bad thing, but two different data types, for sure. A C string is a char array.
how do you go about doing a c string
closed account (E0p9LyTq)
change your name member variable from string name to something like char name[256]. The exact number of array elements should be whatever you believe will contain the maximum number of characters (plus one) your nation's name will be. I use 256 because no real world nation has a name larger than 255 characters. C strings need a null '\0' terminator to work properly with the C string library functions.

Also change your #include <string> to #include <cstring> (preferred method in C++ code) or
#include <string.h> .

I would personally recommend you look over the C string library in the reference section here to get familiar with what the library functions do, and don't do. http://www.cplusplus.com/reference/cstring/

After playing around with the examples, then go back and continue working on your Country class.

After your code works the way you want it go back and rewrite the code for C++ strings.
ok so i have this so far. I'm so confused, can someone at least start me off with this problem, like maybe write half of it just to get me started??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example program
#include <iostream>

#include <cstring>
using namespace std;


class Country {
  char name [256];
  long population;
  double area;
};
  
  Country (string name, long population, double area) {
    name = usa; 
    population= 8888; 
    area=12345; 
}
closed account (E0p9LyTq)
C strings can't be assigned as simply as C++ strings. You have to do a STRing CoPY (look for a function in the C string library).

Where is your constructor definition in your class definition? What you believe to be a constructor is a simple non-class function, and that function will not be able to access your class data members.

Here is an example of how I might rewrite your code snippet. It is quick and dirty and not the best code:

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
#include <iostream>
#include <cstring>

class Country
{
public:
   char itsName[256];
   unsigned long itsPopulation;
   double itsArea;

public:
   // constructors/destructor
   Country();
   Country(char[], unsigned long, double);
   ~Country() {}
};

Country::Country()
{
   // C strings require using the C string library functions
   strcpy(itsName, "No Name");
   itsPopulation = 0;
   itsArea = 0.0;
}

Country::Country(char nation_name[], unsigned long nation_population, double nation_area)
{
   // C strings require using the C string library functions
   strcpy(itsName, nation_name);
   itsPopulation = nation_population;
   itsArea = nation_area;
}


// test program
int main()
{
   // use the first (blank parameter) constructor to creat this object
   Country null_nation;

   std::cout << "null_nation's name is " << null_nation.itsName << std::endl;

   // use the second constructor to create this object, specifiying the data members
   Country utopia("Utopia", 50, 150);

   std::cout << "utopia's name is " << utopia.itsName << "\n";

   return 0;
}


Normally I would not have the class data members be public access, I would make them private: and write setter/getter class methods to change/retrieve the values.
thanks a lot for all of your help. I really appreciate you getting me started on this. I think I understand what to do now, other then the qsort.
You picked a challenging problem. C-style strings are not the easiest things to work with. If you get stuck again, I suggest trying a simpler version of the problem, such as just sorting an array of C-style strings. Also, make sure you understand qsort doing simple sorts first.
ok thanks a lot! :)
Topic archived. No new replies allowed.