1 Replies - 5068 Views - Last Post: 15 November 2009 - 02:17 PM Rate Topic: -----

#1 sakatari   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 05-November 08

Printing out a Vector of Structs

Posted 15 November 2009 - 02:13 PM

Hi, I'm trying to get a vector of structs to print out but for some reason i keep getting this error:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'main::data' (or there is no acceptable conversion)

If someone could help that would be great- Here is my code:

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


void main(){


	ifstream csv("grades.csv");
	char c=' ';
	string line; 
	int count=0;
	

	struct data{
		string name;
		string classname;
		string grade;
	};

	vector <data> datarecords;
	data newdata;
	
	if (!csv.is_open()){
		cout << "Could not open file" << endl; 
		return;
		}

	cout << "File is open" << endl;


	while (!csv.eof()){

		csv.get(c);

		if (c = ',' && count==0){
			newdata.name=line;
			count++;
			line=" ";
		}
		else if (c= ',' && count==1){
			newdata.classname= line;
			count++;
			line=" ";
		}
		else {
			newdata.grade=line;
			line=" ";
			datarecords.push_back(newdata);
		}
		line=line+c;
	}

	for (int x=0; x < datarecords.size(); x++){
		 cout << datarecords[x] << endl;
	
	}

	

}








Is This A Good Question/Topic? 0
  • +

Replies To: Printing out a Vector of Structs

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,234
  • Joined: 14-September 07

Re: Printing out a Vector of Structs

Posted 15 November 2009 - 02:17 PM

You have a couple options. The first is to overload the << operator (generally done for classes, but you could do it for a struct). Option two is to explicitly call the members you want printed from that particular index.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1