use of struct

why cant i use the function deletecontactname and deletecontactroll? actually i want to put any deleted or garbaged contact bottom most ... please help...
also tell me whether i can use a single function to search a contact be name and roll..i tried to take input choice from the user where the user selects from name and roll but it didn't work ;got some compilation error.
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
 #include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
#define MAX 5
typedef 
		struct contact{string name; long roll; float cpi;} info;
		
void inputcontact (contact student[])
{
	char flag='y';int i=0;
	while(i<MAX &&flag=='y')	
	{
		cout<<"name: \n";
		cin>>student[i].name;
		cout<<"roll number: \n";
		cin>>student[i].roll;
		cout<<"CPI: \n";
		cin>>student[i].cpi;
		i++;
		cout<<"input another? (y/n) ";
		cin>>flag;
	}
}

void showcontact (contact student [])
{ 	cout<<"Name                   Roll no.          CPI   \n";
	for(int i=0;i<MAX;i++)
	{
		cout<<student[i].name<<"                     ";
		cout<<student[i].roll<<"           ";
		cout<<student[i].cpi<<"      \n";
	}
}

int findname(contact student[],string nam)
{	int idx=-1;
	for(int i=0;i<MAX;i++)
	{
		student[i].name==nam;
		idx=i;break;
	}
	return idx;
}

int findroll(contact student[],float rol)
{	int idx=-1;
	for(int i=0;i<MAX;i++)
	{
		student[i].roll==rol;
		idx=i;break;
	}
	return idx;
}
	
void updatecontactroll(contact student[],float roll1)
{int idx=-1;char flag;contact tmp;
	findroll(student,roll1);
	if(idx==-1) cout<<"contact not found. ";
	else
	{
		cout<<"name: \n";
		cin>>tmp.name;
		cout<<"roll number: \n";
		cin>>tmp.roll;
		cout<<"CPI: \n";
		cin>>tmp.cpi;
	cout<<student[idx].name<<" will be replaced by "<<tmp.name<<" are you sure to replace?(y/n)";
	cin>>flag;
	if(flag!='n')	student[idx]=tmp;cout<<"sucessfully replaced.\n";
	}
	
}

void updatecontactname(contact student[],string name1)
{int idx=-1;char flag;contact tmp;
	findname(student,name1);
	if (idx==-1)cout<<"contact not found. ";
	else
	{
		cout<<"name: \n";
		cin>>tmp.name;
		cout<<"roll number: \n";
		cin>>tmp.roll;
		cout<<"CPI: \n";
		cin>>tmp.cpi;
	cout<<student[idx].name<<" will be replaced by "<<tmp.name<<" are you sure to replace?(y/n)";
	cin>>flag;
	if(flag!='n')	student[idx]=tmp;cout<<"sucessfully replaced.\n";
	 }
	 
	
}

void swap(contact *p1, contact *p2)
{
	string tmp;tmp=p1->name;p1->name=p2->name;p2->name=tmp;
	long tm;tm=p1->roll;p1->roll=p2->roll;p2->roll=tm;
	float t;t=p1->cpi;p1->cpi=p2->cpi;p2->cpi=t;
}

void deletecontactname(contact student[],string name1)
{
	int idx=-1;char flag;
	idx=findname(student,name1);
	if(idx==-1)cout<<"contact not found. ";
	else
	{
	cout<<student[idx].name<<" is going to be deleted.are you sure?(y/n)\n";
	cin>>flag;
	if(flag!='n')
		{
			for(int i=idx;i<MAX;i++)
			{
				swap(student[i],student[i+1]);
			}
			student[MAX-1].name='N';
			student[MAX-1].roll=0;
			student[MAX-1].cpi=0;
		}
	}
	
}

void deletecontactroll(contact student[],long roll1)
{
	int idx=-1;char flag;
	idx=findroll(student,roll1);
	if(idx==-1)cout<<"contact not found. ";
	else
	{
	cout<<student[idx].name<<" is going to be deleted.are you sure?(y/n)\n";
	cin>>flag;
	if(flag!='n')
		{
			for(int i=idx;i<MAX;i++)
			{
				swap(&student[i],&student[i+1]);
			}
			student[MAX-1].name='N';
			student[MAX-1].roll=0;
			student[MAX-1].cpi=0;
		}
	}
	
}


int main()
	{
		contact freshy[MAX];string name1;long roll1;
		for(int i=0;i<MAX;i++)
		{
		 freshy[i].name='N';
		 freshy[i].roll=0;
		 freshy[i].cpi=0;
		}
		char choice;
		while(1)
{
		cout<<"i for input contact,s for showing all contacts,u to update a contact,d to delete a contact. \n";
		cin>>choice;
		switch(choice)
		{
			case 'i':
				inputcontact(freshy);
				break;
			case 's':
				showcontact(freshy);
				break;
			case 'u':
				cout<<"n to search by name, r to search by roll number \n";
				char up;
				cin>>up;
				switch(up)
				{
					case 'n':
						cout<<"give the name: \n";
						cin>>name1;
						updatecontactname(freshy,name1);
						break;
					case 'r':	
						cout<<"give the roll number: \n";
						cin>>roll1;
						updatecontactroll(freshy,roll1);
						break;
				
			    }
				case 'd':
						cout<<"n to search by name, r to search by roll number \n";
				char upd;
				cin>>upd;
				switch(upd)
				{
					case 'n':
						cout<<"give the name: \n";
						cin>>name1;
						deletecontactname(freshy,name1);
						break;
					case 'r':	
						cout<<"give the roll number: \n";
						cin>>roll1;
						deletecontactroll(freshy,roll1);
						break;    
							
				}
				
				break;	
		}
}
}
	
		
								
		
			
	
Topic archived. No new replies allowed.