Write a series of statements that accomplish each of the following. Assume that
ID: 3824153 • Letter: W
Question
Write a series of statements that accomplish each of the following. Assume that we've defined class Person that contains the private data members char lastName[15]; char firstName[10]; int age; int id; and public member functions//accessor functions for id void setld(int); int get ld() const;//accessor functions for lastName void setlastName(const string&); string getLastName() const;//accessor functions for firstName void setFirstName(const string&); string getFirstName() const;//accessor functions for age void setAge(int); int getAge() const; Also assume that any random access files have been opened properly. a) Initiate nameage dat with 100 records that store values lastName ="unassigned", firstName = ** and age = 0. b) Input 10 last names, first names and ages, and write them to the file. c) Update a record that already contains information. If the record does not contain information, inform the user "No info". d) Delete a record that contains information by reinitializing that particular record.Explanation / Answer
aANS)
// blank_Person()
Person blankPerson();
blankPerson.setlastName("aunassigned"); // unassigned
blankPerson.setFirstName( ""); // firstname
blankPerson.setAge("0"); // age
blankPerson.id =0; // id
for ( int r=0 ; r< 100; r++ ) // for
fileObject.write( reinterpret_cast< const char * >( dblankPerson ), sizeof( Person ) );
bANS::
string last; // last
string first; // first
string age; // age
int id;
int counter 1;
while ( counter<=10 )
{
do
{
cout << " enter id number for new record (1-100): "
cin << id;
} while ((id < 1)|| (id > 100 )) ;
fileObject.ssekg((id-1)* sizeof(Person));
Person person;
if(person.getId() == 0)
{
cout << " enter last name, first name, and age: " ;
cin >> setw(15) last;
cin >> setw(15) first;
fileObject.seekp((id-1) * sizeof(Person));
fileObject.write(
reinterpret_cast<const char * > (&person), sizeof(Person));
counter++;
}
else
cerr << "Record #" << id << "already contains data." <<end1;
}
cANS:
string last; // lastname
string first; // first name
string age; // age
int id;
do
{
cout << "Enter id number for new record(1-100): "
cin << id;
) while ( ( id < 1 ) || ( id > 100 ) ) :
fileObject.seekg( ( id - 1 ) * sizeof( Person ) );
Person person;
fileObject.read( reinterpret_cast< char * >( &person ), sizeof( Person ) );
if ( person.getId() != 0)
{
cout << "Enter new last name,first name and age: ";
cin >> setw(15) last;
cin >> setw(15 ) first;
cin >> setw(4 ) >> age;
person.setlastName( last );
person. setFirstName( first );
person.setAge( age );
person.setId( id );
fileObject.seekp( ( id -1 ) * sizeof( Person ) ):
}
else
cerr << "no information" << end1;
dANS))
// do
do
{
// enter id number
cout << "Enter id number for new record(1-100):"
cin << id:
}
while((id <1 ) || (id > 100 ));
fileObject.seekg( ( id - 1) * sizeof( Person ) );
Person person;
fileObject.read( reinterpret_cast< char * >( &person ), sizeof( Person ) );
if ( person.id != 0)
{
Person blankPerson();
blankPerson.setLastName("unassigned");
blankPerson.setFirstName("");
blankPerson.setAge("0");
blankPerson.id = 0;
fileObject.seekp((id-1) * sizeof(Person));
fileObject.write(reinterpret_cast< const char * > (&blankPerson, sizeof(Person));
cout<< "Record #" << id << "deleted: << end1;
}
else
cerr << "Record #" << id << "is empty "<< end1;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.