Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I doing a program to make an adressbook but i have problems with my search and delete function. Can you help me? I can choose

I doing a program to make an adressbook but i have problems with my search and delete function. Can you help me? I can choose to either search my contacts in my file from phonenumber or name. How do i solve that through my code? Using c++

#include

#include

#include

#include

#include

using namespace std;

int menu()

{

int choice;

cout << "Welcome to my contactbook! ";

cout << "1: Enter records ";

cout << "2: View records ";

cout << "3: Remove records ";

cout << "4: Search records ";

cout << "5: Exit ";

cin >> choice;

return choice;

}

void addRecord()

{

string name, birthday, adress, phnr, email, other;

int session=0;

int count=0;

while(session!=2)

{

cout << "Enter the first and lastname of the person: ";

cin.ignore(numeric_limits::max(), ' ');

getline(cin, name);

cout << "Enter " << name << " birthday: ";

getline(cin,birthday);

cout << "Enter " << name << " adress: ";

getline(cin,adress);

cout << "Enter " << name << " phone number: ";

getline(cin, phnr);

cout << "Enter " << name << " email: ";

getline(cin, email);

cout << "Enter other information: ";

getline(cin, other);

cout << "--------------";

ofstream phonebook;

phonebook.open("phonebook.txt", ios::app);

phonebook << "Name: " << name << " " << " ";

phonebook << "Birthday: " << birthday << " ";

phonebook << "Adress: " << adress << " ";

phonebook << "Phone number: " << phnr << " ";

phonebook << "Email: " << email << " ";

phonebook << "Other information: " << other << " ";

phonebook << "-------------- ";

phonebook.close();

cout << "Would you like to add more records? ";

cout << " Type 1. YES or Type 2."<

cout << "Enter your choice:";

cin >> session;

}

}

void viewRecord()

{

string line;

ifstream phonebook("phonebook.txt");

if(phonebook.is_open())

{

while(getline(phonebook, line))

{

cout << line << " ";

}

phonebook.close();

}

else

cout << "Cannot find the file! Are you sure you added records?"<< endl;

}

void removeRecord()

{

cout << "You chose to delete a contact.";

cout << "Which contact would you like to delete? Specify by contact number. ";

}

void searchRecord()

{

char phnr[10];

char phnr1[10];

int found=0;

strcmp ( phnr, phnr1); //(const char * phnr, const char* phnr1, size_t num)

cout << " Enter the phone number of records to search: ";

cin >> phnr;

fstream phonebook;

phonebook.open("phonebook.txt", ios::ate | ios::in |ios::out |ios::binary);

phonebook.seekg(0,ios::beg); //sker frn brjan av buffer

while(phonebook.read((char*)phnr, sizeof(*phnr)))

{

if(strcmp==0)

{

found=1;

viewRecord();

}

}

phonebook.clear();

if(found==0)

{

cout << " ---Record not found--- ";

}

phonebook.close();

}

int main()

{

int option;

do

{

option = menu();

switch(option)

{

case 1: addRecord();

break;

case 2: viewRecord();

break;

case 3: removeRecord();

break;

case 4: searchRecord();

break;

case 5: cout << "Goodbye!" << endl;

break;

default: cout << option << " is an invalid option" << endl;

}

}

while(option!=5);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions