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

Has to be done in C++ and the a1.txt file that is needed In this program you are

ID: 3887359 • Letter: H

Question

Has to be done in C++

and the a1.txt file that is needed

In this program you are required to read in from a text file a list of persons, display their information, and do a search for certain persons. The information of each person consists of. add new persons, Last_Name First_Name (25 chars, max) Address (60 chars, max) Your program will open a fle al.ht and read one person's info at a time, and you may assume the int entries in the file are all in correct forms. Each person's information is collected, in sequence, as name, D, address. You can assume the maximum number of people in al.txt is 20 1. Read from al.txt the information. Each person should take one structure. The structure should be defined as to hold name, ID, and address. 2. Your program prints the information of all people according the ID numbers in ascending order. The information (name, ID, address) should be identical to the information your program read from altt (see sample al.txt as in the same folder of this assignment; you need to follow the same formatting as defined in al.txt) 3. Your program then asks the user if there is additional person to add. If so, the fields of information on this new person are the same format as in al.txt, in sequence of name, D address, and your program asks the user to enter one entry at a time. Your program should continue to ask if additional person is to be added, and the user decides whether or not to add a new one 4. After 3, your program asks the user to search for a person by last name. The user will enter the name and your program should print out the information: name, ID, address, in sequence and one line per field (e.g. name takes one line). Your program should continuously ask the user if to search a new person, and stop only if the user chooses so. If there are two or more people with identical last name, each of them should be listed. Your program prints out the information immediately after the person is found. 5. After 4, your program should have a function to print out all people who are "searched and found", in the order of first searched and first printed. The standard printed (on screen) information consists: naime, ID, address, in sequence and one line per field.

Explanation / Answer

//main.cpp

#include <iostream>

#include <fstream>

#include <stdlib.h>

#include <string.h>

using namespace std;

struct person{

int ID;

string lastName;

string firstName;

string address;

};

int fillStruct(struct person []);

int addPerson(struct person [], int );

void sortID(struct person [], int );

void searchList(struct person [], int );

void printArray(struct person [],int );

int main()

{

int counter=0, run=1;

char selection;

struct person personList[30];

struct person MyPerson;

//populate array from file

counter=fillStruct(personList);

while(run)

{

cout << "Please make your selection: ";

cout << "Add person: A " << "Search list: S " << "Sort list: R " "Print list: P " << "Quit: Q ";

cin >> selection;

switch(selection)

{

case 'A':case 'a':

{

system("cls");

addPerson(personList, counter);

}

break;

case 'S':case 's':

{

system("cls");

searchList(personList, counter);

}

break;

case 'R':case 'r':

{

system("cls");

sortID(personList, counter);

}

break;

case 'P':case 'p':

{

system("cls");

printArray(personList, counter);

}

break;

case 'Q':case 'q':

{

run=0;

}

break;

}

}

return 0;

}

int fillStruct(struct person personList[])

{

ifstream preDefined;

preDefined.open("a1.txt");

cout << "Opened file Begin data parse ";

int counter=0;

while(preDefined.peek()!=EOF)

{

string temp, blank;

getline(preDefined,temp);

personList[counter].ID = atoi(temp.c_str());

getline(preDefined,personList[counter].lastName,',');

getline(preDefined,personList[counter].firstName);

getline(preDefined,personList[counter].address);

getline(preDefined,blank);

counter++;

}

preDefined.close();

return counter;

}

int addPerson(person personList[], int counter)

{

int response=1;

cout << "Add a person ";

cout << " ";

do

{

cin.clear();

cin.sync();

string tempID;

cout << "Input ID: ";

getline(cin,tempID);

personList[counter].ID = atoi(tempID.c_str());

cout << " Input last name: ";

getline(cin,personList[counter].lastName);

cout << " Input first name: ";

getline(cin,personList[counter].firstName);

cout << " Input address: ";

getline(cin,personList[counter].address);

counter++;

cout << " Add another person? Yes: 1 No: 0 ";

cin >> response;

}while(response);

return counter;

}

void searchList(person personList[], int counter)

{

int i, mode;

char searchType;

string query;

size_t found;

cout << "Enter F to search by first name Enter L to search by last name Enter I to search by ID #";

cin >> searchType;

switch(searchType)

{

case 'F':case 'f':

{

cout << "Enter the name you wish to query";

cin >> query;

mode=1;

}

break;

case 'L':case 'l':

{

cout << "Enter the name you wish to query";

cin >> query;

mode=1;

}

case 'I':case 'i':

{

cout << "Enter the ID # you wish to query";

cin >> query;

mode=2;

}

}

switch(mode)

{

case 1:

{

for (int i = 0; i<counter; i++)

{

found = (personList[i].lastName).find(query);

if (found!=string::npos)

{

cout << " ";

cout << "ID: " << personList[i].ID << " ";

cout << "Last Name: " << personList[i].lastName << " ";

cout << "First Name: " << personList[i].firstName << " ";

cout << "Address: " << personList[i].address << " ";

}

}

}

break;

case 2:

{

for (int i = 0; i<counter; i++)

{

if(personList[i].ID==atoi(query.c_str()))

{

cout << " ";

cout << "ID: " << personList[i].ID << " ";

cout << "Last Name: " << personList[i].lastName << " ";

cout << "First Name: " << personList[i].firstName << " ";

cout << "Address: " << personList[i].address << " ";

}

}

}

break;

}

}

void sortID(struct person personList[], int counter)

{

struct person temp;

int i, j;

for(i=0; i<counter; i++)

{

for(j=0; j<counter-1; j++)

{

if(personList[j].ID>personList[j+1].ID)

{

temp=personList[j+1];

personList[j+1]=personList[j];

personList[j]=temp;

}

}

}

for(i=0; i<counter; i++)

{

cout << "ID Number: " << personList[i].ID << " ";

cout << "Last Name: " << personList[i].lastName << " ";

cout << "First Name: " << personList[i].firstName << " ";

cout << "Address: " << personList[i].address << " ";

}

}

void printArray(struct person personList[],int counter)

{

int i;

cout << " ID # First Last Street Address ";

for(i=0;i<counter;i++)

{

cout << " " << personList[i].ID;

cout << " " << personList[i].firstName;

cout << " " << personList[i].lastName;

cout << " " << personList[i].address;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote