This is probably a really easy question, and I know it's starring at me right in the face, but I can't put my finger on it. I'm doing this semi-simple program for school and I think my only problem here is passing my array of structs to a function that writes them to a txt file. It works, the only problem is that it's only writing the last record to the file. Is there something wrong with my loop, and I know I'm not passing the arrays right. Can anyone help me?
Here's my program assignment:
Write a C++ program that declares a struct for representing an AZ motor vehicle operator. It will contain the fields listed. Declare and populate an array of three of these structs and fill the struct variables for each element with data that you hard code in the program. Loop through the array of structs and from inside the loop, pass each element by reference to a function that writes it to a text file.
Write your entire struct variable to the file using a statement of this form:
yourFile.write( (char*) &yourStructVariable, sizeof yourStructVariable);
Here's what I have so far......
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
const char* MVOPERATOR_FILE = "C:\\Users\\Public\\Documents\\motorVehicleOperator.txt\0" ; // Declare and assign the motorVehicleOperator.txt
// file to a CONSTANT CHAR variable.
const int MAX_NAME = 15 ;
const int MAX_ADDRESS = 20 ;
const int MAX_STATE = 3 ;
const int MAX_ZIP = 6 ;
const int MAX_PHONE = 12 ;
const int MAX_DATE = 10 ;
const int MAX_CODE = 2 ;
const int MAX_ENDORSE = 10 ;
const int MAX_RESTRICT = 10 ;
struct operatorDataType
{
char lastName[MAX_NAME] ;
char firstName[MAX_NAME] ;
char middleName[MAX_NAME] ;
char numberAndStreet[MAX_ADDRESS] ;
char addressLine2[MAX_ADDRESS] ;
char city[MAX_NAME] ;
char stateOrProvince[MAX_STATE] ;
char postalCode[MAX_ZIP] ;
char number[MAX_PHONE] ;
char expirationDate[MAX_DATE] ;
char dateOfBirth[MAX_DATE] ;
char dateIssued[MAX_DATE] ;
char classCode[MAX_CODE] ;
char endorsements[MAX_ENDORSE] ;
char restrictions[MAX_RESTRICT] ;
} ;
struct operatorDataType operatorRecord[3] ;
void writeData( operatorDataType &operatorRecord[] ) ;
int main()
{
// Fill the member list for the first element in the operatorRecord array.
strcpy(operatorRecord[0].lastName, "Z*****") ;
strcpy(operatorRecord[0].firstName, "Heather") ;
strcpy(operatorRecord[0].middleName, "Marie") ;
strcpy(operatorRecord[0].numberAndStreet, "1234 N.") ;
strcpy(operatorRecord[0].addressLine2, "Monopoly Way") ;
strcpy(operatorRecord[0].city, "Tucson") ;
strcpy(operatorRecord[0].stateOrProvince, "AZ") ;
strcpy(operatorRecord[0].postalCode, "85743") ;
strcpy(operatorRecord[0].number, "123-4567") ;
strcpy(operatorRecord[0].expirationDate, "10/23/2048") ;
strcpy(operatorRecord[0].dateOfBirth, "10/23/1983") ;
strcpy(operatorRecord[0].dateIssued, "11/15/1999") ;
strcpy(operatorRecord[0].classCode, "D") ;
strcpy(operatorRecord[0].endorsements, "None") ;
strcpy(operatorRecord[0].restrictions, "None") ;
// Fill the member list for the second element in the operatorRecord array.
strcpy(operatorRecord[1].lastName, "Z*****") ;
strcpy(operatorRecord[1].firstName, "Derek") ;
strcpy(operatorRecord[1].middleName, "Michael") ;
strcpy(operatorRecord[1].numberAndStreet, "1234 N.") ;
strcpy(operatorRecord[1].addressLine2, "Operator Way") ;
strcpy(operatorRecord[1].city, "Tucson") ;
strcpy(operatorRecord[1].stateOrProvince, "AZ") ;
strcpy(operatorRecord[1].postalCode, "85743") ;
strcpy(operatorRecord[1].number, "123-4567") ;
strcpy(operatorRecord[1].expirationDate, "05/17/2048") ;
strcpy(operatorRecord[1].dateOfBirth, "05/17/1983") ;
strcpy(operatorRecord[1].dateIssued, "01/20/2010") ;
strcpy(operatorRecord[1].classCode, "D") ;
strcpy(operatorRecord[1].endorsements, "None") ;
strcpy(operatorRecord[1].restrictions, "None") ;
// Fill the member list for the third element in the operatorRecord array.
strcpy(operatorRecord[2].lastName, "W******") ;
strcpy(operatorRecord[2].firstName, "Cara") ;
strcpy(operatorRecord[2].middleName, "Christine") ;
strcpy(operatorRecord[2].numberAndStreet, "12345 N.") ;
strcpy(operatorRecord[2].addressLine2, "Candyland Ave.") ;
strcpy(operatorRecord[2].city, "Tucson") ;
strcpy(operatorRecord[2].stateOrProvince, "AZ") ;
strcpy(operatorRecord[2].postalCode, "85737") ;
strcpy(operatorRecord[2].number, "123-4567") ;
strcpy(operatorRecord[2].expirationDate, "05/10/2060") ;
strcpy(operatorRecord[2].dateOfBirth, "05/10/1988") ;
strcpy(operatorRecord[2].dateIssued, "07/14/2005") ;
strcpy(operatorRecord[2].classCode, "D") ;
strcpy(operatorRecord[2].endorsements, "None") ;
strcpy(operatorRecord[2].restrictions, "None") ;
// Using a loop to pass each element of the operatorRecord array to the writeData function.
/*for (int i = 0; i < 3; i++)
{
writeData ( operatorRecord[i] ) ;
}*/
// Using a loop to pass each element of the operatorRecord array to the writeData function.
for (int i = 0; i < 3; i++)
{
cout << "\nThe Record shows the following data: \n\n"
<< "Last Name : " << operatorRecord[i].lastName << "\n"
<< "First Name : " << operatorRecord[i].firstName << "\n"
<< "Middle Name : " << operatorRecord[i].middleName << "\n"
<< "Address : " << operatorRecord[i].numberAndStreet << "\n"
<< "Address 2 : " << operatorRecord[i].addressLine2 << "\n"
<< "City : " << operatorRecord[i].city << "\n"
<< "State : " << operatorRecord[i].stateOrProvince << "\n"
<< "Postal Code : " << operatorRecord[i].postalCode << "\n"
<< "Number : " << operatorRecord[i].number << "\n"
<< "Exp. Date : " << operatorRecord[i].expirationDate << "\n"
<< "DOB : " << operatorRecord[i].dateOfBirth << "\n"
<< "Date Issued : " << operatorRecord[i].dateIssued << "\n"
<< "Class : " << operatorRecord[i].classCode << "\n"
<< "Endorse. : " << operatorRecord[i].endorsements << "\n"
<< "Restrict. : " << operatorRecord[i].restrictions << "\n\n" ;
writeData ( operatorRecord ) ;
}
system("pause") ;
return 0 ;
}
void writeData ( operatorDataType &operatorRecord[] )
{
ofstream operatorRecordFile ( MVOPERATOR_FILE, ios::out | ios::binary ) ; // Creates an OUTPUT FILE object using the file name.
int i ;
// Attempt to CREATE the file object (operatorRecordFile), and check to see if this
// operation fails. Terminate the program if it does fail.
if ( operatorRecordFile.fail() )
{
cout << "Cannot open " << MVOPERATOR_FILE << ". Program will terminate. \n" ;
}
operatorRecordFile.write( (char*) &operatorRecord, sizeof operatorRecord ) ;
operatorRecordFile.close() ;
}