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");
    }

}

Recommended Answers

All 5 Replies

Instead of making us download your .zip file, post some sample lines of the .dat file, including ones that do work and ones that don't (ie. Truman).

Sorry, still new to this. When I open the .dat file, I just get a bunch of gibberish. This is the closest I can come to giving sample lines..no idea how that could help...but..I still don't know much about coding...

Washington George ý Ä VA VA 1Fed (good one)


Truman Harry S ™ ¡ \ ´ MO MO 1Dem (bad one)

Eisenhower Dwight D ¡ © b ± TX KS 1Rep (Next one after bad one)

For some reason ifstream doesn't work with that file. Change it to FILE pointer and it works ok

//    ifstream inPresFile("presfile.dat");
    FILE* inPresFile = fopen("presfile.dat", "rb"); // <<< THIS

    if (!inPresFile)
    {
        cout<<"File could not be opened"<<endl;
        exit(1);
    }

    heading();
    
    int counter = 0;
    //for (int counter=1; counter < 43; counter++)
    while( fread(reinterpret_cast<char *> (&presRecord),1, sizeof (presRecord), inPresFile))
    {
        ++counter;
        //nameField[0] = '\0';
        //inPresFile.read(reinterpret_cast<char *> (&presRecord),sizeof (presRecord));


        strcpy (nameField,presRecord.firstName);
        strcat (nameField," ");
        strcat (nameField,presRecord.lastName);

Here is the output I got

1         George Washington          Fed   	4/30/1789	3/3/1797	2/22/1732	VA		12/14/1799	VA		1
2         John Adams                 Fed   	3/4/1797	3/3/1801	10/30/1735	MA		7/4/1826	MA		1
3         Thomas Jefferson           D/R   	3/4/1801	3/3/1809	4/13/1743	VA		7/4/1826	VA		1
4         James Madison              D/R   	3/4/1809	3/3/1817	3/16/1751	VA		6/28/1836	VA		1
5         James Monroe               D/R   	3/4/1817	3/3/1825	4/28/1758	VA		7/4/1831	VA		1
6         John Quincy Adams          D/R   	3/4/1825	3/3/1829	7/11/1767	MA		2/23/1848	MA		1
7         Andrew Jackson             Dem   	3/4/1829	3/3/1837	3/15/1767	SC		6/8/1845	TN		1
8         Martin Van Buren           Dem   	3/4/1837	3/3/1841	12/5/1782	NY		7/24/1862	NY		1
9         William Henry Harrison     Whig  	3/4/1841	4/4/1841	2/9/1773	VA		4/4/1841	OH		1
10        John Tyler                 Whig  	4/6/1841	3/3/1845	3/29/1790	VA		1/18/1862	VA		1
11        James K Polk               Dem   	3/4/1845	3/3/1849	11/2/1795	NC		6/15/1849	TN		1
12        Zachary Taylor             Whig  	3/5/1849	7/9/1850	11/24/1784	VA		7/9/1850	KY		1
13        Millard Fillmore           Whig  	7/10/1850	3/3/1853	1/7/1800	NY		3/8/1874	NY		1
14        Franklin Pierce            Dem   	3/4/1853	3/3/1857	11/23/1804	NH		10/8/1869	NH		1
15        James Buchanan             Dem   	3/4/1857	3/3/1861	4/23/1791	PA		6/1/1868	PA		0
16        Abraham Lincoln            Rep   	3/4/1861	4/15/1865	2/12/1809	KY		4/15/1865	IL		1
17        Andrew Johnson             Dem   	4/15/1865	3/3/1869	12/29/1808	NC		7/31/1875	TN		1
18        Ulysses S Grant            Rep   	3/4/1869	3/3/1877	4/27/1822	OH		7/23/1885	NY		1
19        Rutherford B Hayes         Rep   	3/4/1877	3/3/1881	10/4/1822	OH		1/17/1893	OH		1
20        James A Garfield           Rep   	3/4/1881	9/19/1881	11/19/1831	OH		9/19/1881	OH		1
21        Chester A Arthur           Rep   	9/19/1881	3/3/1885	10/5/1829	VT		11/18/1886	NY		1
22        Grover Cleveland           Dem   	3/4/1885	3/3/1889	3/18/1837	NJ		6/24/1908	NJ		1
23        Benjamin Harrison          Rep   	3/4/1889	3/3/1893	8/20/1833	OH		3/13/1901	IN		1
24        Grover Cleveland           Dem   	3/4/1893	3/3/1897	3/18/1837	NJ		6/24/1908	NJ		1
25        William McKinley           Rep   	3/4/1897	9/14/1901	1/29/1843	OH		9/14/1901	OH		1
26        Theodore Roosevelt         Rep   	9/14/1901	3/3/1909	10/27/1858	NY		1/6/1919	NY		2
27        William Howard Taft        Rep   	3/4/1909	3/3/1913	9/15/1857	OH		3/8/1930	VA		1
28        Woodrow Wilson             Dem   	3/4/1913	3/3/1921	12/28/1856	VA		2/3/1924	DC		2
29        Warren G Harding           Rep   	3/4/1921	8/2/1923	11/2/1865	OH		8/2/1923	OH		1
30        Calvin Coolidge            Rep   	8/3/1923	3/3/1929	7/4/1872	VT		1/5/1933	VT		1
31        Herbert Hoover             Rep   	3/4/1929	3/4/1933	8/10/1874	IA		10/20/1964	IA		1
32        Franklin Delano Roosevelt  Dem   	3/4/1933	4/12/1945	1/30/1882	NY		4/12/1945	NY		1
33        Harry S Truman             Dem   	4/12/1945	1/20/1953	5/8/1884	MO		12/26/1972	MO		1
34        Dwight D Eisenhower        Rep   	1/20/1953	1/20/1961	10/14/1890	TX		3/28/1969	KS		1
35        John F Kennedy             Dem   	1/20/1961	11/22/1963	5/29/1917	MA		11/22/1963	VA		1
36        Lyndon B Johnson           Dem   	11/22/1963	1/20/1969	8/27/1908	TX		1/22/1973	TX		1
37        Richard M Nixon            Rep   	1/20/1969	8/9/1974	1/9/1913	CA		4/22/1994	CA		1
38        Gerald R Ford              Rep   	8/9/1974	1/20/1977	7/14/1913	NE		0/0/0			1
39        James Earl Carter, Jr      Dem   	1/20/1977	1/20/1981	10/1/1924	GA		0/0/0			1
40        Ronald W Reagan            Rep   	1/20/1981	1/20/1989	6/12/1924	IL		0/0/0			2
41        George H W Bush            Rep   	1/20/1989	1/20/1993	6/12/1924	MA		0/0/0			1
42        William Jefferson Clinton  Dem   	1/20/1993	1/20/2001	8/19/1946	AR		0/0/0			1
43        George W Bush              Rep   	2/20/2001	8/9/1974	7/6/1946	TX		0/0/0			1
commented: Thanks, helped a bunch +2

Thanks man, I really appreciate your time. I got it working now. I'll have to ask the teacher about the .dat file. I wonder if he just gave us a bum file...since I only see ifstream in my notes.

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.