Each time the program goes through a loop, nameField gets bigger and bigger. Strcat keeps adding new names to the field. How can I clear the field after each output? I thought it was something like nameField = {' '};...but that doesn't work. There has to be a simply way to clear everything from the nameField variable. I have spent way to much time trying to figure this out.

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

void outputLine(char, char);

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[45];


    ifstream inPresFile("\\Users\\Owner\\Documents\\presfile.dat",ios::in);

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


    for (int counter=1; counter < 40; counter++)
    {
        inPresFile.read(reinterpret_cast<char *> (&presRecord),sizeof (presRecord));


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

        cout<<setw(3)
            <<setiosflags(ios::left)
            <<counter
            <<setw(15)
            <<nameField


            <<presRecord.heldOfficeBeg.month
            <<'/'
            <<presRecord.heldOfficeBeg.day
            <<'/'
            <<presRecord.heldOfficeBeg.year
            <<"'\n"
            <<presRecord.heldOfficeEnd.month
            <<'/'
            <<presRecord.heldOfficeEnd.day
            <<'/'
            <<presRecord.heldOfficeEnd.year;
        //nameField = {' '};
        system ("pause");
    }

Try namefield[0] = '\0';

commented: Helped with homework. +2

Thank you, that did it. I should have asked a long time ago.

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.