0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by thelamb
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.