A little problem with my assignment on a telephone directory

How do i validate the strings where if someone accidentally types a number in the name category , it says "Try again"
also how do i add a sorting program which if i input "a" , tells me details of people whose name start with "a"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//Telephone directory system

#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

class Directory
{
 char name[20];
 char num[30];
 char address[50];
 public:
 void enter()
  {
   cout<<"Enter name: "<<endl;
   gets(name);
   cout<<"Enter number: "<<endl;
   gets(num);
   cout<<"Enter address: "<<endl;
   gets(address);
  }
void show()
  {
    cout<<"Name - "<<name<<endl;
    cout<<"Number - "<<num<<endl;
    cout<<"Address - "<<address<<endl;
  }
  char *rname()
  {
   return name;
  }
  char *radd()//to return address
  {
   return address;
  }
 };

void create()
  {
   fstream F;
   F.open("phonedirectory.dat",ios::binary|ios::app);
   Directory D; char ch;
   do
   {
    D.enter();
    F.write((char*)&D,sizeof(D));
    cout<<"\nDo you want to enter more(y/n)\n";
    cin>>ch;
   }
   while(ch=='y');
   F.close();
  }
void display()
  {
  fstream F;
  F.open("phonedirectory.dat",ios::binary|ios::in);
  Directory D;
  cout<<"Displaying all the records\n";
  while(F.read((char*)&D,sizeof(D)))
   {
     D.show();
   }
   getch();
   F.close();
  }

void searchn()//search by name
  {
   fstream F;
   Directory D;
   char name[20];
   int found=0;
   F.open("phonedirectory.dat",ios::binary|ios::in);
   cout<<"Enter name to be searched - ";
   gets(name);
   while(F.read((char*)&D,sizeof(D)))
   {
    if(strcmpi(name, D.rname())==0)
      {
       cout<<"Record Found"<<endl<<endl;
       D.show();
       found++;
      }
   }
   if(!found)
    {
    cout<<name<<endl<< "Not found "<<endl;
    }
   F.close();
   getch();
  }
void insrec()
  {
   int choice;
   fstream F;
   F.open("phonedirectory.dat",ios::binary|ios::app);
   Directory D;
   do
   {
    cout<<"Enter the details for new record "<<endl;
    D.enter();
    F.write((char*)&D,sizeof(D));
    cout<<"Do you want to enter more records "<<endl;
    cout<<"1. Yes  "<<"2. No  "<<endl;
    cin>>choice;
   }
   while(choice==1);
   F.close();
   sortname();
  }
void delrec()
  {
   char delname[30];             //for checking the record to be deleted
   fstream F1,F2;
   F1.open("phonedirectory.dat",ios::binary|ios::in);
   F2.open("temp.dat",ios::binary|ios::app);
   Directory D;
   int del=0;//checking if name found
   cout<<"Enter the name of the record you want to Delete - ";
   gets(delname);
   while(F1.read((char*)&D,sizeof(D)))
    {
      if((strcmpi(delname,D.rname()))!=0)
	 {
	  F2.write((char*)&D,sizeof(D));
	 }
       else
       del++;
    }
	 if(del)
	 cout<<"The record has been deleted";

	 F1.close();
	 F2.close();
	 remove("phonedirectory.dat");
	 rename("temp.dat","phonedirectory.dat");
	 sortname();
	 getch();
  }
int main()
  {
   clrscr();
   int choice;
   int t;
   int opt;
   char pass[20];
   cout<<"********* WELCOME *********"<<endl;
   cout<<"********* TO THE *********"<<endl;
   cout<<"********* PHONE DIRECTORY *********"<<endl;
   cout<<endl<<endl<<endl;
   cout<<"SELECT YOUR IDENTITY"<<endl;
   cout<<"1.VIEWER"<<endl;
   cout<<"2.ADMINISTRATOR"<<endl;
   cout<<"CHOICE - ";
   cin>>opt;
   if(opt==1)
     {
	do
       {
	clrscr();
	cout<<endl<<endl;
	cout<<"What would you like to do - "<<endl;
	cout<<"1.Search record"<<endl
	<<"2.Display record"<<endl
	<<"3.Emergency numbers"<<endl
	<<"4.Exit"<<endl;
	cout<<"Enter choice : \n";
	cin>>t;
	clrscr();
	switch(t)
	{
	case 1:searchn();
	break;
	case 2:display();
	cout<<endl<<endl<<endl;
	break;
	case 3: cout<<"Police - 100"<<endl;
	cout<<"Ambulance - 108"<<endl;
	cout<<"Fire Department - 101"<<endl;
	break;
	case 4:
	cout<<"EXITING THE PROGRAM......"<<endl;
	cout<<"Thank You"<<endl;
	default:
	exit(0);
	}//for switch case
       }
      while(t!=3);//end of while
     }
   else if(opt==2)
     {
      do
      {
	clrscr();
	cout<<"What would you like to do - "<<endl;
	cout<<"1.Add"<<endl
	<<"2.Delete"<<endl
	<<"3.Search"<<endl
	<<"4.Sort"<<endl
	<<"5.Display"<<endl
	<<"6.Exit"<<endl;
	cout<<"Enter choice - ";
	cin>>choice;
	clrscr();
	switch(choice)
       {
	case 1:insrec();
	break;
	case 2:delrec();
	cout<<endl<<endl<<endl;
	break;
	case 3:searchn();
	cout<<endl<<endl<<endl;
	break;
	case 4:sortname();
	cout<<endl<<endl<<endl;
	break;
	case 5:display();
	cout<<endl<<endl<<endl;
	break;
	case 6:
	exit(0);
	default: exit(0);
       }//for switch case
     }
      while(t!=3);//end of while
    }
    else
    {
     cout<<"\nExiting...\n";
    }
    getch();
    return 0;
  }
Last edited on
read input as a string and check that it is all letters.

as for how you break the data up or sort it...
sort your container and you can either iterate to find the records that start with a (brute force)
or
sort your container and use binary search (a little tricky as you have to find the first record starting with a, not just any record with a)
or
store the records in a container of containers by first letter

do you need to find substrings (contains letters) or only starts-with conditions? Is starts-with only one letter or possible a string?
Last edited on
Topic archived. No new replies allowed.