how can i put delete function in this program pls

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
#include<iostream>
#include<algorithm> //for std::sort
#include<string>

using namespace std;

struct student
{
  int id;
  string name;
  string nationality;
  string gender; 
};

void insert(student array[],const  unsigned int MAX_STUDENT);  

bool sortByID(const student& a, const student& b)
{
	return ( a.id < b.id );
}


char sortByName(const student& a, const student& b)
{
	return ( a.name < b.name);
}

char sortByNationality(const student& a, const student& b)
{
	return (a.nationality < b.nationality);
}

void mySort(student array[],const unsigned int MAX)
{
	int opt = 0;
	cout<<"Welcome to sorting function please do the following selection"<<endl;
	cout<<"***************************************************"<<endl;
	cout<<"* 1.Sort by student ID"<<endl;
	cout<<"* 2.Sort by student Name"<<endl;
    cout<<"* 3.Sort by Nationality"<<endl;
	cout<<"***************************************************"<<endl;
	cout<<"Selection: ";
	cin >> opt;
	cout<<"\n\n";

	switch(opt)
	{
		case 1: std::sort(array,array+MAX,sortByID); break;
		case 2:	std::sort(array,array+MAX,sortByName); break;
		case 3:	std::sort(array,array+MAX,sortByNationality);break;
	}

}

void display(const student array[], unsigned int MAX);
int main()
{	
	const unsigned int MAX_SIZE = 100;
	student array[MAX_SIZE];

	int option = 0;
	bool exitProgram = false;

	do
	{ 
		cout <<"Welcome to student recording system"<<endl;
		cout <<"Please choose one of the following option\n\n"<<endl;
		cout <<"1.Insert new record"<<endl;
		cout <<"2.Sort record"<<endl;
		cout <<"3.Delete record"<<endl;
		cout <<"4.Display record"<<endl;
		cout <<"0) Exit program\n\n\n";
    
		 cin >> option;

		 switch(option)
		 {
			case 1: insert(array,MAX_SIZE);  break;

			case 2:	cout<<"you picked sorting\n";
					cout<<"Now sorting\n\n\n";
					mySort(array,MAX_SIZE);
					cout<<"Sorting done\n\n\n";
					break;

			case 3: //do delete
			case 4:   display(array,MAX_SIZE); break;

			default : exitProgram = true; break;
		 }

		}while(!exitProgram);     		
	  

	return 0;

}
void insert(student array[],const  unsigned int MAX_STUDENT)
{

	cout<<"\n\n";

  for(unsigned int i = 0 ; i < MAX_STUDENT; i++)
  {

	cout<<"For student #"<<(i+1)<<endl<<endl;;
	cout <<"Enter student ID: ";
    cin >>array[i].id;
    cin.ignore(100,'\n');
	cout<<endl;
    
    cout <<"Enter student name: ";
    cin >>array[i].name;
    cin.ignore(100,'\n');
	cout<<endl;
    
    cout <<"Enter student nationality:  ";
    cin >>array[i].nationality;
    cin.ignore(100,'\n');
	cout<<endl;
    
    cout <<"Enter student gender:  ";
    cin >>array[i].gender;
    cin.ignore(100,'\n');
	cout<<endl;
    }
}

void display(const student array[], unsigned int MAX)
{
	cout<<"\\************************************************\\"<<endl;
	for(unsigned int i = 0; i < MAX; i++)
	{		
		cout<<"---------------------------------------------------"<<endl;;
		cout<<"Student #:"<<(i+1)<<endl;
		cout<<"ID# : "<<array[i].id<<endl;
		cout<<"Name : "<<array[i].name<<endl;
		cout<<"Nationality : "<<array[i].nationality<<endl;		
		cout<<"Gender : "<<array[i].gender<<endl<<endl;
		cout<<"---------------------------------------------------"<<endl;;
	
	}
	cout<<"\n\\************************************************\\"<<endl;
}
This is kind of a strange question. You know how to declare functions correct? You've already defined predicates and have an understanding of the std algorithms. What are you having trouble understanding?

First, what are the requirements? What criteria will you use for searching? You'll probably want to use the find_if algorithm and define predicates to search for students based on name or id. You'll probably need to ask the user whether they want to delete by name or id.
i want to create delete function that will check if a name or id is entered twice and delete one?
You may be able to use an if() function within a loop such as if(i==0||word[i-1]!=word[i] /* this word is different to the previous word so go ahead and print it*/
To delete the contents of an array element assuming it was a string you could specify its content as " " or array[i]=NULL (or empty). Another possibility is to use memmove(p1,p2,n ) to move the affected elements in the array ahead of the duplication down 1 to overwrite the duplication.

rgds
Topic archived. No new replies allowed.