Answer to Question #23538 in C++ for Rohan Saha

Question #23538
A data File contains name and telephone number as two of its fields. Write a program in C++ using an object to do the following:
1.Add records in the file
2.Searching of telephone number(s) for a given name
3.Determine the name if the telephone number is known.
4.Updating the data file,whenever there is a change in telephone number.
A menu to implement above mentioned tasks.
1
Expert's answer
2013-02-01T10:04:43-0500
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include "string.h"
#include <cstring>
#include <vector>
using namespace std;
class tel{
public:
string name;
string num;
void init(string a, string b)
{
name=a;num=b;
}
void print()
{
cout<<"name : "<<name<<"& number : "<<num<<endl;
}

};
void outMENU(){
cout<<"1-Add records in the file\n"<<"2 - Searching of telephone number(s) for a given name\n"
<<"3-Updating the data file\n5-Exit\n";
}

int main()
{
int n=1;
vector <tel> a;
tel temp;
string name, num;
outMENU();
cin>>n;
while (n!=5){
bool out=0;
switch (n){
case 1:

cout<<"Enter name: ";
cin.ignore();
getline(cin,name,'\n');
cout<<"Enter number: ";
getline(cin,num,'\n');
for (size_t i=0;i<a.size();i++){
if (a[i].num==num) {cout<<"Contact exists, name : "<<a[i].name<<endl; out=1;break;}
}
if (out==1) break;
temp.init(name,num);
a.push_back(temp);
break;
case 2:
cout<<"Enter name to search for phone: ";
cin.ignore();
getline(cin,name,'\n');
for (int i=0;i<a.size();i++){
if (a[i].name.find(name)!=string::npos)& a[i].print();
}
break;
case 3:
for (int i=0;i<a.size();i++){
cout<<i<<" ";
a[i].print();
}
cout<<"Enter contact to update : ";
int j;
cin>>j;
if (0<=j<a.size()){
cout<<"Enter name: ";
cin.ignore();
getline(cin,name,'\n');
cout<<"Enter number: ";
getline(cin,num,'\n');
temp.init(name,num);
a.erase(a.begin()+j);
a.insert(a.begin()+j,temp);
}
break;
case 5:
break;
default: break;
}
cout<<endl;
outMENU();
cin>>n;
}
system("PAUSE");
return 0;
}



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS