So, I have to write a program that calculates the five number summary of a .txt file with 2 columns and n rows. A snippet of the file would be
23 7.8
27 17.8
39 26.5
41 27.2

From this I am trying to input the numbers into a vector so I can manipulate them. I am new to vectors and am not sure what I am doing wrong. But, so far here is my code

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

struct five {
	float column1, column2;
	friend istream& operator >> ( istream& ins, five& r);
};
istream& operator >> (istream& ins, five& r) {
	ins >> r.column1 >> r.column2;
	return ins;
}
void main(){
	ifstream input;
	input.open("data.txt");
	if (input.is_open() ){
		vector<five> data;
		five datam;

		while (!input.eof() ) {
			input >> datam;
			data.push_back (datam);
		}
}

I have tried reading tutorials about it, but I still cant figure out what I am doing wrong. For example how do I verify that the input is being assigned correctly to the vector for output, and how would I write a statement to manipulate the data to calculate the mean lets say.

Thanks for the help!

main must return int (not void).
You are missing a closing brace.
"five" is a strange name.
datum is spelled with a u.
See comments in program.

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

struct five {
	float column1, column2;
};

// Does not need to be a friend of five since five's data are public.
istream& operator >> (istream& ins, five& r) {
	ins >> r.column1 >> r.column2;
	return ins;
}

int main(){

	ifstream input( "data.txt" );
	if ( !input )
	{
		cerr << "Cannot open file\n";
		exit( -1 );
	}

	vector<five> data;
	five datum;

	// If you test for eof, THEN read (and don't test again right
	// after) you will add an extra element to the end of the vector.
	// So test for eof something like this.
	while ( input >> datum )
	{
		data.push_back (datum);
	}

	// This loop displays the data.
	// You can be modify it to sum the data instead.
	for( size_t i = 0; i < data.size(); ++i )
	{
		cout << data[i].column1 << ", " << data[i].column2 << endl;
	}
}

> In case you need to use 2D Vectors (this is an alternative to your vector of 'five'-structs) :

1) vector<vector<float> > myVector(5,vector<float>(5)); (this will create a 5x5 vector)

2) You can store data like this: myVector[2][1] = 3; (store the number 3 in the vector)

3) Printing using cout is also possible: cout << myVector[2][1] << endl;

Thanks for the help I finished it after sorting it and getting everything ready. Thanks again for the help, I feel that I now understand vectors better and will be more confident in using them in the future.

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.