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];
public:
	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
	ifile.close();

	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;

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

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

And your problem is...?

When asking for help, you really need to explain why you need help. And "because it's wrong" or "because it's due" are not acceptable.

My question is, where should I be dividing the population by the 1000. In the method or in the main.

My project prints fine, I'm just not understanding how I am to figure out the for loop I need to create to print out the population.

I'm getting rather confused, because I'm also writing this same project in Java.

Does it matter? Try writing the code and maybe we'll have a better idea when we see what you're trying to do.

Suggestion: Initially, ignore the 1000. Just print the data as you see it. You can then add the divide once you get the rest running.

I've already written the code but the nested for loop if you didn't see it above.

I can't figure out the nested for loop to get it to print the population and to then divide.

after you do the getname, do a getpopulation, divide the return value by 1000 and print it ...

I've already written the code but the nested for loop if you didn't see it above.

No, you wrote

for (int i=0; i<nrecs; i++) {
		cout << recs[i].getName(buf) << endl;

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

and I suggested:

Initially, ignore the 1000. Just print the data as you see it. You can then add the divide once you get the rest running.

So finish writing the loop and print some data.

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