.dat file in a program

Hi, programmers.

The Prof. Asked me to do the following:

In a program, you need to store the names and populations of 12 countries. Create arrays to store this information and then write the code needed to read the information into the arrays from a file named pop.dat. Write only the names of the countries to a file named country.dat.

and here is my 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
  #include <iostream>
using namespace std;

class country{
   public:
       string name;
       int population;
       country(string n,int p){
           name = n;
           population = p;
       }
};

int main(){
   country* l = new country[12];
   ifstream infile;
   infile.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
   try {
       infile.open("pop.dat");
       string name;
       int population;
       for (int i = 0; i < 12; i++){
           infile >> name >> population;
           country c(name,population);
           l[i] = c;
       }
       infile.close();
       ofstream outfile;
       outfile.open("country.dat");
       for (int i = 0; i < 12; i++)
           outfile.write(l[i].name+"\n");
       outfile.close();
   }
   catch (std::ifstream::failure e) {
       std::cerr << "The File ---- input.txt ---- is not Here \n";
   }
   return 0;
}


The thig is I still get errors and I don't know why, Did I write exactly what I asked to write or I wrote something else not related?

Who could fix the problem?

Thanks ,,
On line 32: outfile.write(l[i].name+"\n");

You can't write a string like this into the stream.
Try this:
outfile << l[i].name << '\n';
Please post the error messages exactly as they appear in your development environment, then perhaps someone can help explain what the error messages are telling you.

first of all you forgot to include <fstream> and <string> libraries
second mistake is by creating a array of country objects , country which has no default constructor , that is a constructor with no arguments

you could make use of vectors or some STL container to store your country class but i reccomend just use a struct

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
#include <iostream>
#include <fstream> // for std::ifstream and std::ofstream
#include <string> // for std::string
using namespace std;

struct country
{
	int population;
	string name;
};

int main(){
	
	country countries[12]; //access members with "." operator like
                                           // countries[3].population = 100
	                                   //if u allocate dinamically replace dot with "->"
	ifstream in("pop.dat");
	ofstream out("country.dat");

	// do your things

	in.close();
	out.close();

	return 0;
}

Last edited on
g3n0m3 wrote:
second mistake is by treating your data type country as an array. classes are not like structs


No, that part of the code is fine. Classes are very very similar to structs, the only significant difference being by default the members of a struct are public, while for a class they are private.

One thing that is missing is a default constructor. A class with no constructor at all is fine, but if you supply your own, you will often need, as in this case, to supply a default constructor.
Last edited on
Topic archived. No new replies allowed.