Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

My program is written in C++ and is supposed to open files and write information

ID: 672573 • Letter: M

Question

My program is written in C++ and is supposed to open files and write information about US citizens to it and also be able to search through them and edit them. I’ve gotten most of the program done except the searching part, I have to use

struct indexNode {

                        char socialSecNum[10];

                        int locationInFile; //This would be location of the structure in the file

            };

That structure ^ to search through the file.

Here is my program (The EditSSN Function is where I can’t solve it from):

#include <iostream>
#include <fstream>
using namespace std;

// Array sizes
const int SSN_SIZE = 10, NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14; //Constant sizes so when retrieving data junk data is not displayed

void AddSSN(); //Adding data to the file prototype
void DisplaySSN(); //Displaying data to the file prototype
void EditSSN(); //Editing data to the file prototype

// Declare a structure for the record.
struct Info {
char ssn[SSN_SIZE];
char name[NAME_SIZE];
int age;
char address1[ADDR_SIZE];
char address2 [ADDR_SIZE];
char phone[PHONE_SIZE];
};

//Main Function
int main() {
int choice;
do{
cout << "What would you like to do?" << endl;
cout << "Choice 1 - Add a record to the file." << endl;
cout << "Choice 2 - Edit a record in the file." << endl;
cout << "Choice 3 - Display a record in the file." << endl;
cout << "Choice 4 - Exit the program." << endl;
cout << "Please enter in one of the numbers above." << endl;
cin >> choice;
switch(choice)
{
case 1: AddSSN(); break;
case 2: EditSSN(); break;
case 3: DisplaySSN(); break;
default: cout << "Invalid choice, program will now terminate.";
}
}while(choice<=3);

return 0;
}

void AddSSN()
{
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 << "SSN: ";
cin.ignore();
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 << "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));

// 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();

people.open("people.dat", ios::in | ios::binary);

if (!people) {
   cout << "Error opening file. Program aborting. ";

}

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's all the data in the file! ";
people.close();
}

void DisplaySSN()
{
fstream people;
people.open ("People.dat", ios::in);
Info citizen;
people.read(reinterpret_cast<char*>(&citizen),sizeof(citizen));
while(!people.eof())
{
cout << "Social Security Number: " << citizen.ssn << endl;
cout << "Name: " << citizen.name << endl;
cout << "Age: " << citizen.age << endl;
cout << "Phone Number: " << citizen.phone << endl;
people.read(reinterpret_cast<char*>(&citizen),sizeof(citizen));
}
people.close();
}

void EditSSN()
{
fstream people;
people.open("People.dat", ios::in|ios::out);
Info citizen;
int ssn;
int locationInFile;


cout << "Enter the Social Security Number of the Individual you wish to edit: "<< endl;
cin >> ssn;
people.seekg((locationInFile-2), ios::beg);
people.read(reinterpret_cast<char*>(&citizen), sizeof(citizen));
cout << "Social Security Number: " << citizen.ssn << endl;
cout << "Name: " << citizen.name << endl;
cout << "Age: " << citizen.age << endl;
cout << "Phone Number: " << citizen.phone << endl;

cout << "Please enter in the information you would like to update: " << endl;
cout << "Please enter the updated Social Security Number. ATTENTION: If social security number does not need to be updated, please re-enter SSN: " << endl;
cin.ignore();
cin.getline(citizen.ssn,SSN_SIZE);
cout << "Please update the citizen's Name: " << endl;
cin.getline(citizen.name, NAME_SIZE);
cout << "Please update the citizen's Age: " << endl;
cin >> citizen.age;
cout << "Please update the citizen's Phone number: " << endl;
cin.getline(citizen.phone, PHONE_SIZE);
people.seekg(ssn*sizeof(citizen),ios::beg);
people.write (reinterpret_cast<char*>(&citizen),sizeof(citizen));
people.close();
}

Explanation / Answer

Comments added

#include <iostream>
#include <fstream>
using namespace std;
// Array sizes
const int SSN_SIZE = 10, NAME_SIZE = 51, ADDR_SIZE = 51, PHONE_SIZE = 14; //Constant sizes so when retrieving data junk data is not displayed
void AddSSN(); //Adding data to the file prototype
void DisplaySSN(); //Displaying data to the file prototype
void EditSSN(); //Editing data to the file prototype
// Declare a structure for the record.
struct Info {
char ssn[SSN_SIZE];
char name[NAME_SIZE];
int age;
char address1[ADDR_SIZE];
char address2 [ADDR_SIZE];
char phone[PHONE_SIZE];
};
//Main Function
int main() {
int choice;
do{
cout << "What would you like to do?" << endl;
cout << "Choice 1 - Add a record to the file." << endl;
cout << "Choice 2 - Edit a record in the file." << endl;
cout << "Choice 3 - Display a record in the file." << endl;
cout << "Choice 4 - Exit the program." << endl;
cout << "Please enter in one of the numbers above." << endl;
cin >> choice;
switch(choice)
{
case 1: AddSSN(); break;
case 2: EditSSN(); break;
case 3: DisplaySSN(); break;
default: cout << "Invalid choice, program will now terminate.";
}
}while(choice<=3);
return 0;
}
void AddSSN()
{
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 << "SSN: ";
cin.ignore();
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 << "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));
// 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();
people.open("people.dat", ios::in | ios::binary);
if (!people) {
cout << "Error opening file. Program aborting. ";
}
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's all the data in the file! ";
people.close();
}
void DisplaySSN()
{
fstream people;
people.open ("People.dat", ios::in);
Info citizen;
people.read(reinterpret_cast<char*>(&citizen),sizeof(citizen));
while(!people.eof())
{
cout << "Social Security Number: " << citizen.ssn << endl;
cout << "Name: " << citizen.name << endl;
cout << "Age: " << citizen.age << endl;
cout << "Phone Number: " << citizen.phone << endl;
people.read(reinterpret_cast<char*>(&citizen),sizeof(citizen));
}
people.close();
}


//function to search and edit
void EditSSN()
{
fstream people;
people.open("People.dat", ios::in|ios::out);
Info citizen;
int ssn;
int locationInFile;
//reading unique ssn number to fetch record
cout << "Enter the Social Security Number of the Individual you wish to edit: "<< endl;
cin >> ssn;
//fetching all records from file
people.read(reinterpret_cast<char*>(&citizen),sizeof(citizen));
while(!people.eof()){
if(citizen.ssn == ssn){ //checking for our required file
//first printing record details
cout << "Social Security Number: " << citizen.ssn << endl;
cout << "Name: " << citizen.name << endl;
cout << "Age: " << citizen.age << endl;
cout << "Phone Number: " << citizen.phone << endl;
cout << "Please enter in the information you would like to update: " << endl;
cout << "Please enter the updated Social Security Number. ATTENTION: If social security number does not need to be updated, please re-enter SSN: " << endl;
cin.ignore();
//getting information to make update
cin.getline(citizen.ssn,SSN_SIZE);
cout << "Please update the citizen's Name: " << endl;
cin.getline(citizen.name, NAME_SIZE);
cout << "Please update the citizen's Age: " << endl;
cin >> citizen.age;
cout << "Please update the citizen's Phone number: " << endl;
cin.getline(citizen.phone, PHONE_SIZE);
people.seekg(ssn*sizeof(citizen),ios::beg);
//updating the record and saving back to file
people.write (reinterpret_cast<char*>(&citizen),sizeof(citizen));
break;
}
}
//closing file sream
people.close();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote