hey I was having trouble with this problem can I get a working example of it to
ID: 3700833 • Letter: H
Question
hey I was having trouble with this problem can I get a working example of it to study in c++
Midterm Project wrie an address book program that will accomplish the followng 1. Read name and address data from the user either from the keyboard or from a tie 2 While reading the names and addresses, put the names into an appropiate data structure 3. Ater reading names 4. Allow user to write out the container data structure to a comma delimited file, this file is different and addresses, allow user to search for names and change the names or addresses in the container da from the input file The input tie will have the tollowing form, 1 line for the name the next ine tor the address name address addressExplanation / Answer
If you have any doubts, please give me comment...
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
struct AddressBook
{
string name;
string address;
};
int searchNameInd(AddressBook book[], int n, string name)
{
int i;
for (i = 0; i < n; i++)
{
if (book[i].name == name)
{
return i;
}
}
return -1;
}
void writeIntoFile(string file, AddressBook book[], int n){
ofstream out(file.c_str());
int i;
for(i=0; i<n; i++){
out<<book[i].name<<","<<book[i].address<<endl;
}
out.close();
}
int main()
{
AddressBook book[100];
string file;
cout << "Enter filename: ";
cin >> file;
ifstream in(file.c_str());
cout << "Enter filename to modified contents: ";
cin >> file;
if (in.fail())
{
cout << "Unable to open file" << endl;
exit(1);
}
int i = 0;
while (!in.eof())
{
getline(in, book[i].name);
getline(in, book[i].address);
i++;
}
while (1)
{
string name;
cout << "Enter name for edit name/address (-1 to exit): ";
cin >> name;
if (name != "-1")
{
int ind = searchNameInd(book, i, name);
if (ind == -1)
{
cout << "Search name not found" << endl;
}
else
{
int choice;
cout << "****EDIT MENU ****" << endl;
cout << "1. Name 2.Address 3.Both Your option:";
cin >> choice;
string name, address;
switch (choice)
{
case 1:
cout << "Enter name to change: ";
getline(cin, book[ind].name);
getline(cin, book[ind].name);
writeIntoFile(file, book, i);
cout << "Successfully modified name" << endl;
break;
case 2:
cout << "Enter address to change: ";
getline(cin, book[ind].address);
getline(cin, book[ind].address);
writeIntoFile(file, book, i);
cout << "Successfully modified adresss" << endl;
break;
case 3:
cout << "Enter name to change: ";
getline(cin, book[ind].name);
getline(cin, book[ind].name);
cout << "Enter address to change: ";
getline(cin, book[ind].address);
getline(cin, book[ind].address);
writeIntoFile(file, book, i);
cout << "Successfully modified name and adresss" << endl;
break;
default:
cout << "Invalid option! Try again";
}
}
}
else
{
cout << "Thank you!" << endl;
break;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.