954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading from a file into an array of structs.

I need to read data from a file into an array of structs. The info is arranged in the file like so...
Spear of Extreme Whaling
January
1851
Captain
Ahab

Each member of the array has a name, month, year, and first and last name of creator.
This is the beginning of my code:

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

struct artifact
{
	string name;
	string month;
	int year;
	string fname;
	string lname;
};

int main()
{
	artifact allInfo[31];
	char menu;
	char yn;
	int j=0;
	string name;
	string filename;
	string yearTemp;
	ifstream inFile;
	ofstream outFile;
	cout<<"Enter a file name: "<<endl;
	cin>>filename;
	inFile.open(filename.c_str());
	while(!inFile.eof())
	{
		getline(inFile, allInfo[j].name);
		getline(inFile, allInfo[j].month);
		getline(inFile, yearTemp);
		getline(inFile, allInfo[j].fname);
		getline(inFile, allInfo[j].lname);
		allInfo[j].year=atoi(yearTemp.c_str());
		j++;
	}


I've checked to make sure that the file is opening.

oliviars22
Newbie Poster
1 post since Apr 2011
Reputation Points: 10
Solved Threads: 0
 

And now what? What is your question?

Please don't say "it doesn't work".

thelamb
Posting Pro in Training
426 posts since Aug 2008
Reputation Points: 193
Solved Threads: 75
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: