Ok, stuck on homework.
The program is supposed to read a list of presidents from a binary file and display them. I have it all working..except..when it gets to Truman...it just keeps repeating his information. I know its still going in the loop, but for some reason the pointer stays on that record until the end of loop.
I don't understand how it could read the first records just fine..and then break.
I re-downloaded the .dat file just in case that was the problem, but didn't fix anything. I attached it in .zip.
I've already spent so much time on this, so any suggestions would be great.
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
using std::ifstream;
int heading()
{
cout<<setw(10)
<<setiosflags(ios::left)
<<"Pres #"
<<setw(27)
<<"President Name"
<<setw(6)
<<"Party\t"
<<"Start Date\t"
<<"End Date\t"
<<"Birth Date\t"
<<"Birth St\t"
<<"Death Date\t"
<<"Burial St\t"
<<"Wives"
<<endl
<<"======================================================================================================================================================="
<<endl;
}
int main()
{
struct date
{
short year;
short month;
short day;
};
date aDate;
struct president
{
char lastName [15];
char firstName [19];
date heldOfficeBeg;
date heldOfficeEnd;
date birthDate;
date deathDate;
char birthState[3];
char buriedState[3];
char married;
char politicalParty[5];
};
president presRecord;
char nameField[30];
ifstream inPresFile("\\Users\\Owner\\Documents\\presfile.dat",ios::in);
if (!inPresFile)
{
cout<<"File could not be opened"<<endl;
exit(1);
}
heading();
for (int counter=1; counter < 43; counter++)
{
//nameField[0] = '\0';
inPresFile.read(reinterpret_cast<char *> (&presRecord),sizeof (presRecord));
strcpy (nameField,presRecord.firstName);
strcat (nameField," ");
strcat (nameField,presRecord.lastName);
cout<<setw(10)
<<setiosflags(ios::left)
<<counter
<<setw(27)
<<nameField
<<setw (6)
<<presRecord.politicalParty
<<resetiosflags(ios::left)
<<"\t"
//Started term in office
//<<setw(2)
<<presRecord.heldOfficeBeg.month
<<'/'
//<<setw(2)
<<presRecord.heldOfficeBeg.day
<<'/'
//<<setw(4)
<<presRecord.heldOfficeBeg.year
<<"\t"
//Ended Term in Office
<<presRecord.heldOfficeEnd.month
<<'/'
<<presRecord.heldOfficeEnd.day
<<'/'
<<presRecord.heldOfficeEnd.year
<<"\t"
//Birthdate
<<presRecord.birthDate.month
<<'/'
<<presRecord.birthDate.day
<<'/'
<<presRecord.birthDate.year
<<"\t"
//Birth state
<<presRecord.birthState
<<"\t\t"
//Death Date
<<presRecord.deathDate.month
<<'/'
<<presRecord.deathDate.day
<<'/'
<<presRecord.deathDate.year
<<"\t"
//Burial State
<<presRecord.buriedState
<<"\t\t"
//Married
<<presRecord.married
<<endl;
// system ("pause");
}
}