So I have a program I need to make which involves an array. It builds fine and runs. Right now I just need to get the array to read in from file then print it on screen. But only a little over half the array prints (the last half). format of file is:
day/time/count of something
01 - 20:21 - 67
01 - 21:18 - 44
01 - 23:10 - 44
02 - 00:42 - 66
02 - 02:11 - 56
02 - 04:10 - 82

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int a=477;
int i;
char filler;

class Pollution
{
private:
	
public:
	double count[a], day[a], time[a], hr, min;
	void print_data (),
		read_data (ifstream&);
};

class Analysis
{
private:
public:
	int maximum ();
	double average ();
};

void Pollution::read_data (ifstream& readfile)
{ 
	for (i=0;i<a;i++)
	{
		readfile>> day[i] >>filler >>hr >>filler >>min >>filler >>count[i];
		time[i] = hr*100+min;
	}
}

void Pollution::print_data ()
{
for(i=0; i<a; i++)
	{
			cout<< day[i]<<":"<<time[i]<<":"<<count[i]<<endl;
	}
}


int main()
{
	Pollution co2;
	ifstream infile ("C:\\pollution_level.txt");

	co2.read_data (infile);
	co2.print_data ();
}

Don't forget to close your code tag :)

First, there is no reason to use global variables here. It is just a bad habit that will bite you at some point. Second, you are asking it to read 'a' lines (477). The example data you posted is only a few lines, so it just dumps a bunch of garbage. You should definitely read until the end of the file - not an arbitrary number of lines.

You could read all of the lines like this:
http://programmingexamples.net/index.php?title=CPP/IO/ReadingLines

then use a std::stringstream to parse it.

Hope that helps,

David

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.