Combine the functionality of the 2 programs bellow to do the following: 1. Enter
ID: 3690453 • Letter: C
Question
Combine the functionality of the 2 programs bellow to do the following:
1. Enter data about people and write the data to a file per the same functionality as program 1.
2. Read and display the contents of the file created above per the same functionality as program 2.
3. The functionality should be in one program. That is, after the user has entered as many personnel record data entries as desired, the program should read the data from the file and display to the screen.
Program 1
// This program uses a structure variable to store a record to a file.
#include <iostream>
#include< fstream>
using namespace std;
// Array sizes
const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;
// Declare a structure for the record.
struct Info
{
char name[NAME_SIZE];
int age;
char address1[ADDR_SIZE];
char address2[ADDR_SIZE];
char phone[PHONE_SIZE];
};
int main()
{
Info person; // To hold info about a person
char again; // To hold Y or N
// Open a file for binary output.
fstream people("people.dat", ios::out | ios::binary);
do
{
// Get data about a person.
cout << "Enter the following data about a "
<< "person: ";
cout << "Name: ";
cin.getline(person.name, NAME_SIZE);
cout << "Age: ";
cin >> person.age;
cin.ignore(); // Skip over the remaining newline.
cout << "Address line 1: ";
cin.getline(person.address1, ADDR_SIZE);
cout << "Address line 2: ";
cin.getline(person.address2, ADDR_SIZE);
cout << "Phone: ";
cin.getline(person.phone, PHONE_SIZE);
// Write the contents of the person structure to the file.
people.write(reinterpret_cast<char *>(&person),
sizeof(person));
// Find out whether the user wants to write another record.
cout << "Do you want to enter another record? ";
cin >> again;
cin.ignore(); // Skip over the remaining newline.
} while (again == 'Y' || again == 'y');
// Close the file.
people.close();
return 0;
}
Program 2
// This program uses a structure to read a record from a file.
#include <iostream>
#include <fstream>
using namespace std;
cons int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;
// Declare a structure for the record.
struct Info
{
char name[NAME_SIZE];
int age;
char address1[ADDR_SIZE];
char address2[ADDR_SIZE];
char phone[PHONE_SIZE];
};
int main()
{
info person;
char again;
fstream people;
// Open the file for input in binary mode.
people.open(“people.dat”, ios::in | ios::binary);
// Test for errors.
if (!people)
{
cout << “Error opening file. Program aborting. ”;
return 0;
}
cout << “Here are the people in the file: ”
// Read the first record from the file.
people.read(reinterpret_cast<char *>(&person),
sizeof(person));
// While not at the end of the file, display
// the records.
while (!people.eof())
{
// Display the record.
cout << “Name: “;
cout << person.name << endl;
cout << “Age: “;
cout << person.age << endl
cout << “Address line 1: “;
cout << person.address1 << endl;
cout << “Address line 2: “;
cout << person.address2 << endl;
cout << “Phone: “;
cout << person.phone << endl;
// Wait for the user to press the Enter key.
cout << “ Press the Enter key to see the next record. ”;
cin.get(again);
// Read the next record from the file.
people.read(reinterpret_cast<char *>(&person),
sizeof(person));
}
cout << “That all the data in the file! ”;
people.close();
return 0;
}
Explanation / Answer
Hi I have combined both in one program. Please test it.
// This program uses a structure variable to store a record to a file.
#include <iostream>
#include<fstream>
using namespace std;
// Array sizes
const int NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14;
// Declare a structure for the record.
struct Info
{
char name[NAME_SIZE];
int age;
char address1[ADDR_SIZE];
char address2[ADDR_SIZE];
char phone[PHONE_SIZE];
};
int main()
{
Info person; // To hold info about a person
char again; // To hold Y or N
// Open a file for binary output.
ofstream people("people.dat", ios::out | ios::binary);
do
{
// Get data about a person.
cout << "Enter the following data about a "
<< "person: ";
cout << "Name: ";
cin.getline(person.name, NAME_SIZE);
cout << "Age: ";
cin >> person.age;
cin.ignore(); // Skip over the remaining newline.
cout << "Address line 1: ";
cin.getline(person.address1, ADDR_SIZE);
cout << "Address line 2: ";
cin.getline(person.address2, ADDR_SIZE);
cout << "Phone: ";
cin.getline(person.phone, PHONE_SIZE);
// Write the contents of the person structure to the file.
people.write(reinterpret_cast<char *>(&person),
sizeof(person));
// Find out whether the user wants to write another record.
cout << "Do you want to enter another record? ";
cin >> again;
cin.ignore(); // Skip over the remaining newline.
} while (again == 'Y' || again == 'y');
// Close the file.
people.close();
// reading file
Info personRead;
//char again1;
ifstream people1;
// Open the file for input in binary mode.
people1.open("people.dat", ios::in | ios::binary);
// Test for errors.
if (!people1)
{
cout << "Error opening file. Program aborting. ";
return 0;
}
cout << "Here are the people in the file: ";
// Read the first record from the file.
people1.read(reinterpret_cast<char *>(&personRead),
sizeof(personRead));
// While not at the end of the file, display
// the records.
while (!people1.eof())
{
// Display the record.
cout << "Name: ";
cout << personRead.name << endl;
cout << "Age: ";
cout << personRead.age << endl;
cout << "Address line 1: ";
cout << personRead.address1 << endl;
cout << "Address line 2: ";
cout << personRead.address2 << endl;
cout << "Phone: ";
cout << personRead.phone << endl;
// Wait for the user to press the Enter key.
cout << " Press the Enter key to see the next record. ";
cin.get(again);
// Read the next record from the file.
people1.read(reinterpret_cast<char *>(&personRead),
sizeof(personRead));
}
cout << "That all the data in the file! ";
people1.close();
return 0;
}