question telephone directory

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 3306 Views - Last Post: 01 October 2012 - 07:12 PM Rate Topic: -----

#1 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

question telephone directory

Posted 29 September 2012 - 10:55 AM

assalaoalaikum
i have been given the following assignmnt
and m really confused about how to do searching and insertion
Consider a personal telephone directory application that reads name/telephone pairs from a file and allows the user to search for a particular person's phone number.
Capital small both allowed

Here is a sample run:

1 - Read phone list
2 - Search for a phone number
3 - Quit

=> 1

Enter phone file: => phone.txt
21 records read...

=> 2
Enter person's name: => Tom Cruise
Tom Cruise, 567-8901

=> 2
Enter person's name: => Shahid Afridi
Sorry, no directory for Shahid Afridi

phone.txt

Larry Radon 345-5671
Susan_Verlin 431-7788
Marla_Hawkens 245-4566
Etc…………………

Specifications:

The design should make use of the prepackaged dynamic array class studied in lab task 1.The Array class must contain the following operation:
1.add an element
2.search an element
3. display array
4. remove element
5. increaseSize() to increase size if required during insertion
6.update/edit an element

Create an application class to represent telephone directory and provide the necessary member functions for operations that array class can’t handle.

In main create at least 5 different records from user and place them in a txt file each record per line .The name of text file must be entered by user at runtime.(e.g. hello.txt Or d:\\file.txt etc)

The program then displays the menu as given in example above.

When the user selects read operation the program then read all the records from file one by one and store them in the array class and display the total no. of records read as well as all the records on the screen.

When the user selects search operation, the program should ask user to enter person name he wants to search and the program then use search function and display the results accordingly as mentioned above in sample run.

Provides all the necessary operators to be overload in your application class for the operations not supported by array class. (For e.g two of them would be stream insertion and extraction operator must need to be overload to provide object display and input functionality).

i am having about 11 errors and warnings in this code
type name expected
type name expected in operator >> and <<


#ifndef header_h
#define header_h
template <class t>
class Array
{
public:
char*name;
long tel;
int nElement;
void setname(char* nn)
void settele(long tt);
char* getname();
long gettele();
Array();
void add(TD &t);
void display(Array &a);
int search(Array &a1,t nme);
void edit(t,t);
void display2();
};
#endif 




#include"header.h"
#include<conio.h>
template <class t>
Array<t>::Array()
{
name=new char[20];
tel=0;
nElement=0;
}
template <class t>
void Array<t>::add(TD &t)
{
a=t;
nElement++;
}
template<class t>
void Array<t>::display(Array &a)
{
for(int i=0;i<5;i++)
{
cout<<a;
cout<<endl;
}
}
template<class t>
int Array<t>::search(Array &a1,t nme)
{
int ans;
for(int j=0;j<size;j++)
{
if(a1==tl)
{
cout<<a1.getname()<<a1.gettele();
ans=j;
}
else
ans=-1;
}
if(ans==j)
return j;
if(ans==-1)
cout<<endl<<"Sorry no directory for "<<nme<<endl;
return ans;
}
template<class t>
void Array<t>::edit(t Original,t New)
{
for(int k=0;k<size;k++)
if(arr[k]==Original)
arr[k]=New;
}
template<class t>
void Array<t>::display2()
{
for(int l=0;l<size;l++)
{
cout<<arr[l];
cout<<endl;
}
} 



#include"header.h"
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
class TD
{
public:
void Array<t>::setname(char* nn)
{
strcpy(name,nn);
}
void Array<t>::settele(long tt)
{
tel=tt;
}
char* Array<t>::getname()
{
return name;
}
long Array<t>::gettele()
{
return tel;
}
void writefile(TD p,fstream fout)
{
fout<<p.getname()<<'\t'<<p.gettele()<<endl;
}
void readfile(TD p1,fstream fin)
{
int y=0;
char*nme=new char[20];
long tele;
fin>>nme>>tele;

cout<<nme<<'\t'<<tele<<endl;
}
TD& operator=(TD &rhs)
{
strcpy(name,rhs.getname());
}
TD& operator==(TD& rhs1)
{
 if (nam==rhs1.setname(nn))
 return nn;
 else
 return 0;
}
friend ostream &operator<<( ostream &, TD & );
friend istream &operator>>( istream &, TD & );
};
ostream& operator<<( ostream &output, TD &num )
  {
	  output << num.Array<t>::getname()<<'\t'<<num.Array<t>::gettelephone()<<endl;
	  return output;
  }
istream& operator>>( istream &input, TD &num1)
 {
	  char*nnn;
	  long ttt;
	  input>>nnn>>ttt;
	  num1.Array<t>::setname(nnn) ;
	  num1.Array<t>::settelephone(ttt);
	  return input;
  }
void main()
{
clrscr();
TD t[5];
char*str=new char[20];
fstream fout,fin;
cin>>str;
fout.open(str,ios::out);//enter file name from user
for(int ii=0;ii<5;ii++)
{
writefile(t[ii],fout);//wirting records
}
Array <TD>a[5];
clrscr();
		  cout<<"\n*****ASSALAMOALAIKUM*****\n";
		  cout<<"1) Read phone list\n";
		  cout<<"2) Search for a phone number\n";
		  cout<<"3) Quit\n";
		  cout<<"Please enter your choice : "<<endl;
char choice;
cin>>choice;
switch(choice)
{
case 1:  //read phone list
{
cout<<"enter phone file =>";
cin>>str;
fin.open(str,ios::in);
if (!fin)
{
cout <<"Can't open input file. " <<endl;

}
for(int i=0;i<5;i++)
{
t[i].readfile(t[i],fin);
}
cout<<i<<endl;
}
break;
case 2:  //search by name
{
char* nam=new char[20];
cout<<"enter person's name => ";
gets(nam);
fin.open (str, ios::in);
for(int j=0;j<5;j++)
{
if(nam==t[j])
t[j].Array<t>::search(a[j],nam);
else
cout<<"Sorry,the directory does not exist "<<endl;
}
break;
case 3:  //Quit
{
		  char exit_char;
		  cout <<"\nPress any key and <enter> to exit \n";
		  cin>> exit_char;
		  return 0;
 }
}
getch();
}


Is This A Good Question/Topic? 0
  • +

Replies To: question telephone directory

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 625
  • Joined: 11-June 11

Re: question telephone directory

Posted 29 September 2012 - 11:31 AM

Either I have forgotten C++ or your code is really messy and lacks standards...

First: Its int main() and return 0; not void main().

It would be easy for us to help if you post the exact lines of errors and warnings.

Besides that, I have noticed that you have declared a class Array in the beginning which contains declarations and you have defined those stuff in another class named TD. I don't think there is any technique in C++ which allows this..
Was This Post Helpful? 1
  • +
  • -

#3 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 29 September 2012 - 12:14 PM

View PostAKMafia001, on 29 September 2012 - 11:31 AM, said:

Either I have forgotten C++ or your code is really messy and lacks standards...

First: Its int main() and return 0; not void main().

It would be easy for us to help if you post the exact lines of errors and warnings.

Besides that, I have noticed that you have declared a class Array in the beginning which contains declarations and you have defined those stuff in another class named TD. I don't think there is any technique in C++ which allows this..


OK
ill try and implement what u have suggested
and about declaring member functions of array class in another class actualy my teacher told me to do so. this is really confusing i hope i get it right
Was This Post Helpful? 0
  • +
  • -

#4 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 01:32 AM

can anyone plx hellp me understand the logic of this program please!!!!!!!!!!m really new to data structures
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1766
  • View blog
  • Posts: 3,422
  • Joined: 30-May 10

Re: question telephone directory

Posted 30 September 2012 - 06:57 AM

I don't know where to begin here.

For starters, you need to work on code layout (see indentation)

Second, you're using an obsolete compiler (most likely TurboC++). This is typified by use of old style includes and the lack of namespaces.
002 #include<conio.h>
This is a header file specific to your compiler.

003 #include<iostream.h>
004 #include<fstream.h>
These are old-style C++ header files.

005 #include<string.h>
006 #include<stdio.h>
These are old-style C header files.

Third, what is the purpose of your templated array? All it seems to be doing is making things more complicated and confusing.

Step 1 would be
class TE {  // a telephone entry
  public:
    // methods to read/write the data
  private:
    string name; // NOT char*
    string number; // NOT an integer
};

TE Directory[10];  // A directory of 10 numbers



Step 2 would be
vector<TE> Directory;

Step 3, if it is part of your assignment, is replacing vector with your own "Array" container.
Was This Post Helpful? 1
  • +
  • -

#6 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 07:34 AM

leae jst tell me this one thing how many objects shud i make of array class and TD class
and my neww code is

#ifndef header_h
#define header_h
template <class t>
class Array
{
int size;
int nElement;
t* arr;
public:
Array(int n);
void add(t e);
void display();
int search(t e);
void edit(t,t);
void display2();
};
#endif


#include"header.h"
#include<conio.h>
template <class t>
Array<t>::Array(int n)
{
size=n;
nElement=0;
arr=new t[size];
for(int i=0;i<size;i++)
{
arr[i]=nElement;
}
}
template <class t>
void Array<t>::add(t e)//INSERTING
{
for(int o=0;o<5;o++)
{
arr[o]=e;
}
}
template<class t>
void Array<t>::display()
{
for(int i=0;i<size;i++)
{
cout<<arr[i];
cout<<endl;
}
}
template<class t>
int Array<t>::search(t e)//SEARCHING
{
int ans;
for(int j=0;j<size;j++)
{
if(arr[j]==e)
ans=j;

else
ans=-1;
}
if(ans==j)
return j;
if(ans==-1)
cout<<endl<<"Not found";
return ans;
}
template<class t>
void Array<t>::edit(t Original,t New)//EDITING
{
for(int k=0;k<size;k++)
if(arr[k]==Original)
arr[k]=New;
}
template<class t>
void Array<t>::display2()
{
for(int l=0;l<size;l++)
{
cout<<arr[l];
cout<<endl;
}
}


//PERSONAL TELEPHONE DIRECTORY SYSTEM
#include"header.h"
#include<conio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
class TD
{
char*name;
long tel;

public:

TD()
{
name=new char[20];
tel=0;
}
void setname(char*n)
{
strcpy(name,n);
}
void settel(long t)
{
tel=t;
}
char* getname()
{
return name;
}
long gettel()
{
return tel;
}
void writefile(TD []t,fstream fout);

void readfile(TD []t,fstream fin);

TD& operator=(TD []t)
{
strcpy(e,name);
e=tel;
}
/*TD& operator==(TD& rhs1)
{
 if (nam==rhs1.setname(nn))
 return nn;
 else
 return 0;
} */
friend ostream &operator<<( ostream &, TD  &);
friend istream &operator>>( istream &, TD & );
};
void TD::writefile(TD []t,fstream fout)
{
fout<<t.getname()<<'\t'<<t.gettel()<<endl;
}
void TD::readfile(TD []t,fstream fin)
{
char*nme=new char[20];
long tele;
fin>>nme>>tele;
t.setname(nme);t.settel(tele);
cout<<nme<<'\t'<<tele<<endl;
}

ostream& operator<<( ostream &output, TD &num )
  {
	  output << num.getname()<<'\t'<<num.gettel()<<endl;
	  return output;
  }
istream& operator>>( istream &input, TD &num1)
 {
	  char*nnn;
	  long ttt;
	  input>>nnn>>ttt;
	  num1.setname(nnn) ;
	  num1.settel(ttt);
	  return input;
  }
void main()
{
clrscr();
TD t[5];
Array <TD> arr(5);
char*str=new char[20];
fstream fout,fin;
cin>>str;
fout.open(str,ios::out);//enter file name from user
for(int ii=0;ii<5;ii++)
{
writefile(t[ii],fout);//wirting records
}
clrscr();
		  cout<<"\n*****ASSALAMOALAIKUM*****\n";
		  cout<<"1) Read phone list\n";
		  cout<<"2) Search for a phone number\n";
		  cout<<"3) Quit\n";
		  cout<<"Please enter your choice : "<<endl;
char choice;
cin>>choice;
switch(choice)
{
case 1:  //READ PHONE LIST
{
cout<<"enter phone file => ";
cin>>str;
fin.open(str,ios::in);
for(int i=0;i<5;i++)
{
readfile(t[i],fin);
e=t[i]; //STORING FILE DATA IN ARRAY CLASS
arr.add(e);
}
cout<<i<<" RECORDS ARE STORED "<<endl;

}
if (!fin)
{
cout <<"Can't open input file. " <<endl;

}

break;
case 2:  //SEARCHING BY NAME
{
char* nam=new char[20];
cout<<"enter person's name => "; //GETTING A NAME
gets(nam);
fin.open (str, ios::in);
for(int j=0;j<5;j++) //SEARCHING
{
if(nam==t[j])
{
}
else
cout<<"Sorry,the directory does not exist "<<endl;
}
break;
case 3:  //QUIT
{
		  char exit_char;
		  cout <<"\nPress any key and <enter> to exit \n";
		  cin>> exit_char;
		  return 0;
 }
}
getch();
}


also please give me an idea on how to search a telephon number frm array

This post has been edited by Skydiver: 30 September 2012 - 07:39 AM
Reason for edit:: Fixed code tags. Please learn to use the properly: [ c o d e ] without the slash at the start and [ / c o d e ] with the slash at the end.

Was This Post Helpful? 0
  • +
  • -

#7 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 08:17 AM

shud i search array by storing file data in array or by copying all data of telephone dir classs in array?
also i have to overload = and == operators in telephone directory and m not sure where they will be needed

and if i have to copy data frm telephn dirctry to array class how do i copy telephone directory data in array class
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,466
  • Joined: 05-May 12

Re: question telephone directory

Posted 30 September 2012 - 10:30 AM

Ideally, the very first line of your directory file should contain how many records there are. That way you read the first line, to figure out how big to make your array. If you don't have that option, your next choices are either do a first pass through the file counting records, allocate the array, and then a second pass to actually load the data; or dynamically grow your array as you read in more data -- double the size of the array each time you need to grow the the array is a common heuristic.

I'm surprised that you know how to do operator overloading but you don't know how to copy data into an array or do a search through it. How many classes did you miss?
Was This Post Helpful? 1
  • +
  • -

#9 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 10:57 AM

hmmm ok thanks for the help actually the problem wid me is dat maybe i hav weak concepts but i nevr missed any classes
and m trying my best :'(
ill do what said plx pray for me

how can v stor 5 recods(name and telephone) in array class if it has only one object?
Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: question telephone directory

Posted 30 September 2012 - 11:01 AM

Hi, you want a class for a telephone entry as was proposed previously. The class is used as an element of Array - so is the template type. The class will contain one name and one phone number.

You also want a class for the telephone directory. This class will contain a pointer to an Array object, which will be created dynamically. The array will store a list/array of telephone entries.

You don't need to start your programming at changing the size of the array so just set it to 50 or 100 initially.


Edit: strike-out is wrong, from the instructions you just need an Array object which can be resized.

Can you use initialization lists?

This post has been edited by #define: 30 September 2012 - 11:15 AM

Was This Post Helpful? 1
  • +
  • -

#11 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 11:26 AM

owwwwwwwwww thanuuuu so much i owe u 1 i forgot to makwe it dynamically now dat i remember my teacher said the same thing
Was This Post Helpful? 0
  • +
  • -

#12 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 11:33 AM

no i dont think v ill use initialiser list but by reading the question do u think their is any need of it?
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: question telephone directory

Posted 30 September 2012 - 11:34 AM

Array might need to be dynamic if it cannot be initialized. Could you add a default constructor to Array or can you use initialization lists?

The Array class uses dynamic memory allocation internally.
Was This Post Helpful? 0
  • +
  • -

#14 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 11:44 AM

ok wait ill check if it needs any initialisation list

oh i hav anothr question
will dat dynamically created pointer point towards array class object??????????/
Was This Post Helpful? 0
  • +
  • -

#15 uzumakinaruto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 29-September 12

Re: question telephone directory

Posted 30 September 2012 - 11:58 AM

ok i have created TD class pointer dynamically
Array <TD> a1(5);
TD *a=new TD[5];

is it ok

also what will the pointer do ????????
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2