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

hey can I get a working example of this problem so I can get a better understand

ID: 3700836 • Letter: H

Question

hey can I get a working example of this problem so I can get a better understanding

Write an address book program that will accomplish the following: 1. Read name and address data from the user either from the keyboard or from a file 2. While reading the names and addresses, put the names into an appropriate data structure 3. After reading names and addresses, allow user to search for 4. Allow user to write out the containe or addresses in the container data structure names and change the names r data structure to a comma delimited file, this file is different from the input file. The input file will have the following form, 1 line for the name the next line for the address name address name address Use features of c++ that you have l learned so far, such as classes, and dynamic data structures. You may not use the STL data structures. Your program will also be evaluated regarding readability and structure (functions and methods). also be

Explanation / Answer

Here is the required code for you. Everything is implemented as per the needs. Go through the comments to understand what’s happening in each block. Defined a structure to hold the address record( name and address) and defined an array of those structs. Drop a comment if you have doubts. Thanks

//code.cpp

#include<iostream>

#include<fstream>

using namespace std;

//structure to store an address field

struct Address{

                string name;

                string address;

};

/*method to read data from file, fill the address array,

* and returns the count of addresses read */

int readFromFile(char fileName[],Address *addressBook){

                int count=0;

                ifstream inFile(fileName);

                if(!inFile){

                                cout<<"File not found"<<endl;

                                return 0;

                }

                string line,name,address;

                //reading line by line

                while(getline(inFile,line)){

                                name=line;

                                getline(inFile,line);

                                address=line;

                                //creating address with input values

                                Address addr;

                                addr.name=name;

                                addr.address=address;

                                //adding to the array

                                addressBook[count]=addr;

                                //incrementing the count

                                count++;

                }

                return count;

}

//method to display the menu

void displayMenu(){

                cout<<"1. Display address book"<<endl;

                cout<<"2. Search for name"<<endl;

                cout<<"3. Change name/address"<<endl;

                cout<<"4. Save data to a csv file"<<endl;

                cout<<"5. Exit"<<endl;

                cout<<"## Enter your choice: ";

}

//method to display the address book

void displayAddressBook(Address addressBook[], int count){

                cout<<"Address Book:"<<endl;

                for(int i=0;i<count;i++){

                                cout<<"Name: "<<addressBook[i].name<<endl;

                                cout<<"Address: "<<addressBook[i].address<<endl;

                                cout<<endl;

                }

}

//method to search for a name in address book, returns the index of

// the element if found, else -1

int search(Address addressBook[], int count, string name){

                for(int i=0;i<count;i++){

                                if(addressBook[i].name==name){

                                                return i;

                                }             

                }

                return -1;

}

//method to change name or address of an address record with current

//name- name

void changeField(Address *addressBook, int count, string name){

                int index=search(addressBook,count,name);

                if(index==-1){

                                cout<<"Specified address not found in the book"<<endl;

                }else{

                                char choice;

                                cout<<"What do you want to change? enter a for address, n for name: ";

                                cin>>choice;

                                if(choice=='a'){

                                                string addr;

                                                cout<<"Enter new address: ";

                                                cin>>addr;

                                                addressBook[index].address=addr;

                                                cout<<"Address changed"<<endl;

                                }else if(choice=='n'){

                                                string name;

                                                cout<<"Enter new name: ";

                                                cin>>name;

                                                addressBook[index].name=name;

                                                cout<<"Name changed"<<endl;

                                }

                }

}

//method to save data to a csv file

void saveToFile(Address *addressBook, int count, char *filename){

                ofstream outFile(filename);

                if(!outFile){

                                cout<<"Error in saving to file"<<endl;

                }else{

                                for(int i=0;i<count;i++){

                                outFile<<addressBook[i].name<<",";

                                outFile<<addressBook[i].address<<endl;

                                }

                                cout<<"data has been saved to "<<filename<<" as comma seperated values"<<endl;

                }

}

int main(){

                //array of address

                Address addressBook[100];

                //reading records

                int count=readFromFile("address.txt",addressBook);

                if(count==0){

                                return 1;

                }

                //loops until user wishes to quit

                int choice=0;

                while(choice!=5){

                                displayMenu();

                                cin>>choice;

                                string text;

                                char outputfilename[50];

                                int index;

                                switch(choice){

                                                case 1:

                                                                displayAddressBook(addressBook,count);

                                                                break;

                                                case 2:

                                                                cout<<"Enter name: ";

                                                                cin>>text;

                                                                index=search(addressBook,count,text);

                                                                if(index==-1){

                                                                                cout<<"Not found!"<<endl;

                                                                }else{

                                                                                cout<<"Found!"<<endl;

                                                                                                cout<<"Name: "<<addressBook[index].name<<endl;

                                                                                                cout<<"Address: "<<addressBook[index].address<<endl;

                                                                }

                                                                break;

                                                case 3:

                                                                cout<<"Enter name: ";

                                                                cin>>text;

                                                                changeField(addressBook,count,text);

                                                                break;

                                                case 4:

                                                                cout<<"Enter output file name: ";

                                                                cin>>outputfilename;

                                                                saveToFile(addressBook,count,outputfilename);

                                                                break;

                                                case 5:

                                                                cout<<"Bye!"<<endl;

                                                                break;

                                                default:

                                                                cout<<"Invalid choice"<<endl;

                                                               

                                }

                }

               

}

//address.txt

Alice

Wonderland

Bob

Bob mansion

Chris

Christapur

Dave

Daffodils

Eric

Westeros

Floyd

Floyd Villa

//OUTPUT

1. Display address book

2. Search for name

3. Change name/address

4. Save data to a csv file

5. Exit

## Enter your choice: 1

Address Book:

Name: Alice

Address: Wonderland

Name: Bob

Address: Bob mansion

Name: Chris

Address: Christapur

Name: Dave

Address: Daffodils

Name: Eric

Address: Westeros

Name: Floyd

Address: Floyd Villa

1. Display address book

2. Search for name

3. Change name/address

4. Save data to a csv file

5. Exit

## Enter your choice: 2

Enter name: Chris

Found!

Name: Chris

Address: Christapur

1. Display address book

2. Search for name

3. Change name/address

4. Save data to a csv file

5. Exit

## Enter your choice: 3

Enter name: Dave

What do you want to change? enter a for address, n for name: n

Enter new name: David

Name changed

1. Display address book

2. Search for name

3. Change name/address

4. Save data to a csv file

5. Exit

## Enter your choice: 1

Address Book:

Name: Alice

Address: Wonderland

Name: Bob

Address: Bob mansion

Name: Chris

Address: Christapur

Name: David

Address: Daffodils

Name: Eric

Address: Westeros

Name: Floyd

Address: Floyd Villa

1. Display address book

2. Search for name

3. Change name/address

4. Save data to a csv file

5. Exit

## Enter your choice: 4

Enter output file name: abcdef.csv

data has been saved to abcdef.csv as comma seperated values

1. Display address book

2. Search for name

3. Change name/address

4. Save data to a csv file

5. Exit

## Enter your choice: 5

Bye!