I need some guidance on an assignment I'm working on for school. The objectives of this assignment are to read data from a binary file, use some basic class functionality, store objects in an array and produce a neatly formatting report.

#include <fstream>
#include <iostream>
#include <cstring>
using std::ifstream;
using std::strcpy;
using std::ios;
using std::cout;
using std::endl;
using std::setw;

class country {
	char cname[20];
	int pop[16];
	char* getName(char* name) {
		strcpy(name, cname);
		return name;
	int getPopulation(int y) {
		if (y<1986 || y>2001) return 0;

void doReport(country recs[], long nrecs);

int main(void) {
	ifstream ifile("popdata.bin", ios::binary);
	if (!ifile) { return 1; }

	//find out how many records are in the file
	ifile.seekg(0L, ios::end);
	long numrecs = ifile.tellg() / sizeof(country);
	country records[numrecs];

	//don't forget to get back to start of file
	ifile.seekg(0L, ios::beg);

	//read data in
	ifile.read(reinterpret_cast<char *>(records), numrecs * sizeof(country));

	//close file

	doReport(records, numrecs);

	return 0;

void doReport(country recs[], long nrecs) {
	char buf[20];
	cout << "Population data 1990 - 1995 (expressed in millions)\n" << endl;
        cout << "        Country    1990    1991    1992    1993    1994    1995" << endl;
        cout << "---------------     -----    -----     -----     -----     -----     -----" << endl; 
	for (int i=0; i<nrecs; i++) {
		cout << recs[i].getName(buf) << endl;

[TEX]      psuedocode that i'm having a hard time with:
  //      for each year in required range
         //print record's data for that year[/TEX]

a hint given: The population data is expressed in thousands, but you have to display it expressed in millions. You will have to divide the values given by 1000 before displaying them.

The final report should look like this: http://www.kishwaukeecollege.edu/faculty/dklick/cis250/sampleOutput02.html

This article has been dead for over six months. Start a new discussion instead.