Please add a delete function to my code to delete a specific record entered by t
ID: 3760155 • Letter: P
Question
Please add a delete function to my code to delete a specific record entered by the user, and compress the binary file after deleting it.
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Array sizes
const int SSN_SIZE = 10, NAME_SIZE = 51, PHONE_SIZE = 14;
// Function prototypes
void Add();
void Update();
void Display();
// Declare a structure for the record.
struct Info
{
char ssn[SSN_SIZE];
char name[NAME_SIZE];
int age;
char phone[PHONE_SIZE];
};
// Declare a structure for the index node
struct indexNode
{
char socialSecNum[10];
int locationInFile; //This would be location of the structure in the file
};
int main()
{
// Variable declaration
int choice;
// Create menu choices
do {
cout << "Menu ";
cout << "1. Add New Citizen Data ";
cout << "2. Update Citizen Data ";
cout << "3. Display Citizen Data ";
cin >> choice;
// Utilize switch statements in order to go through the various functions
switch(choice)
{
// Add function calling
case 1: Add();
break;
// Update function calling
case 2: Update();
break;
// Display function calling
case 3: Display();
default: cout << "Invalid Choice";
}
}
// If the choice is not one of the three listed, the program will not run
while (choice<=3);
return 0;
}
// Add Function to add citizen data to the binary file
void Add()
{
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 | ios::app);
do {
// Get data about a person from the user
cout << "Enter the following data about a " << "person: ";
cout << "SSN: ";
cin.ignore(); // Skip over the remaining newline.
cin.getline(person.ssn, SSN_SIZE);
cout << "Name: ";
cin.getline(person.name, NAME_SIZE);
cout << "Age: ";
cin >> person.age;
cin.ignore(); // Skip over the remaining newline.
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));
// Determine 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();
}
// Display Function Calling
void Display()
{
char again;
Info person; // To hold info about a person within the display function
fstream people;
// Open the file for input
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 << "Social Security Number: ";
cout << person.ssn << endl;
cout << "Name: ";
cout << person.name << endl;
cout << "Age: ";
cout << person.age << endl;
cout << "Phone: ";
cout << person.phone << endl;
// Wait for the user to press the Enter key.
cout << " Press the Enter key twice to see the next record. ";
cin.get(again);
cin.ignore();
// Read the next record from the file.
people.read(reinterpret_cast<char *>(&person),sizeof(person));
}
cout << "That's all the data in the file! " << endl;
// Close the file
people.close();
}
// Update Function Calling
void Update()
{
fstream people;
// Open the file for input and output
people.open("people.dat", ios::in| ios::out| ios::binary);
Info person; // To hold info about a person
int ssn;
long recNum; // Declare a variable to hold the record number
// Ask user to enter the record number that they wish to update
cout << "Enter the record number of the citizen you wish to edit: "<< endl;
cin >> recNum;
// Move to the record and read it
people.seekg((recNum*sizeof(person)), ios::beg);
people.read(reinterpret_cast<char*>(&person), sizeof(person));
// Display the record contents
cout << "Social Security Number: " << person.ssn << endl;
cout << "Name: " << person.name << endl;
cout << "Age: " << person.age << endl;
cout << "Phone Number: " << person.phone << endl;
// Get the new record data
cout << "Please enter in the information you would like to update: " << endl;
cout << "Please enter the updated Social Security Number: ";
cin.ignore();
cin.getline(person.ssn,SSN_SIZE);
cout << "Please update the name: ";
cin.getline(person.name, NAME_SIZE);
cout << "Please update the age: ";
cin >> person.age;
cout << "Please update the phone number: ";
cin.ignore();
cin.getline(person.phone, PHONE_SIZE);
// Move back to the beginning of this record's position
people.seekp(recNum*sizeof(person),ios::beg);
// Write the new record over the current record
people.write (reinterpret_cast<char*>(&person),sizeof(person));
people.close();
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
// Array sizes
const int SSN_SIZE = 10, NAME_SIZE = 51, PHONE_SIZE = 14;
// Function prototypes
void Add();
void Update();
void Display();
// Declare a structure for the record.
struct Info
{
char ssn[SSN_SIZE];
char name[NAME_SIZE];
int age;
char phone[PHONE_SIZE];
};
// Declare a structure for the index node
struct indexNode
{
char socialSecNum[10];
int locationInFile; //This would be location of the structure in the file
};
int main()
{
// Variable declaration
int choice;
// Create menu choices
do {
cout << "Menu ";
cout << "1. Add New Citizen Data ";
cout << "2. Update Citizen Data ";
cout << "3. Display Citizen Data ";
cin >> choice;
// Utilize switch statements in order to go through the various functions
switch(choice)
{
// Add function calling
case 1: Add();
break;
// Update function calling
case 2: Update();
break;
// Display function calling
case 3: Display();
default: cout << "Invalid Choice";
}
}
// If the choice is not one of the three listed, the program will not run
while (choice<=3);
return 0;
}
// Add Function to add citizen data to the binary file
void Add()
{
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 | ios::app);
do {
// Get data about a person from the user
cout << "Enter the following data about a " << "person: ";
cout << "SSN: ";
cin.ignore(); // Skip over the remaining newline.
cin.getline(person.ssn, SSN_SIZE);
cout << "Name: ";
cin.getline(person.name, NAME_SIZE);
cout << "Age: ";
cin >> person.age;
cin.ignore(); // Skip over the remaining newline.
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));
// Determine 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();
}
// Display Function Calling
void Display()
{
char again;
Info person; // To hold info about a person within the display function
fstream people;
// Open the file for input
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 << "Social Security Number: ";
cout << person.ssn << endl;
cout << "Name: ";
cout << person.name << endl;
cout << "Age: ";
cout << person.age << endl;
cout << "Phone: ";
cout << person.phone << endl;
// Wait for the user to press the Enter key.
cout << " Press the Enter key twice to see the next record. ";
cin.get(again);
cin.ignore();
// Read the next record from the file.
people.read(reinterpret_cast<char *>(&person),sizeof(person));
}
cout << "That's all the data in the file! " << endl;
// Close the file
people.close();
}
// Update Function Calling
void Update()
{
fstream people;
// Open the file for input and output
people.open("people.dat", ios::in| ios::out| ios::binary);
Info person; // To hold info about a person
int ssn;
long recNum; // Declare a variable to hold the record number
// Ask user to enter the record number that they wish to update
cout << "Enter the record number of the citizen you wish to edit: "<< endl;
cin >> recNum;
// Move to the record and read it
people.seekg((recNum*sizeof(person)), ios::beg);
people.read(reinterpret_cast<char*>(&person), sizeof(person));
// Display the record contents
cout << "Social Security Number: " << person.ssn << endl;
cout << "Name: " << person.name << endl;
cout << "Age: " << person.age << endl;
cout << "Phone Number: " << person.phone << endl;
// Get the new record data
cout << "Please enter in the information you would like to update: " << endl;
cout << "Please enter the updated Social Security Number: ";
cin.ignore();
cin.getline(person.ssn,SSN_SIZE);
cout << "Please update the name: ";
cin.getline(person.name, NAME_SIZE);
cout << "Please update the age: ";
cin >> person.age;
cout << "Please update the phone number: ";
cin.ignore();
cin.getline(person.phone, PHONE_SIZE);
// Move back to the beginning of this record's position
people.seekp(recNum*sizeof(person),ios::beg);
// Write the new record over the current record
people.write (reinterpret_cast<char*>(&person),sizeof(person));
people.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.