i created a delete function to check for similar user id and delete one but it can seems to work, so i need your help please..

#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 delete(student array[]);
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: delete(array); break;
			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;
}

void delete(student array[])
{
	char s2=" ";
	=" ";
	strcpy(s1,s2)
	      " "
}
VernonDozier commented: Way too many threads on the same topic. Lack of effort. Inconsiderate of other people's time. -5
Nick Evan commented: what Vernon said :) -6
tux4life commented: what niek_e said :) -4

Dani AI

Generated

Three quick, practical fixes that solve the symptoms seen in the thread: the broken delete body, the reserved-word conflict, and the “array always full” problem.

First, avoid naming a function delete (as noted) and stop trying to copy strings with undefined identifiers like s1/s2 (as pointed out). Prefer a clear name such as removeByID and keep track of how many student records are actually in use rather than assuming every slot is valid. Prefer std::vector for safety and simplicity.

A modern, minimal approach using std::vector (removes all entries that match the id):

void removeByID(std::vector<student>& students, int id) {
    auto it = std::remove_if(students.begin(), students.end(),
                             [id](const student& s){ return s.id == id; });
    students.erase(it, students.end());
}

If an array must be used, maintain a separate count and shift elements left when deleting a single match:

bool removeByID(student arr[], size_t &count, int id) {
    for (size_t i = 0; i < count; ++i) {
        if (arr[i].id == id) {
            for (size_t j = i + 1; j < count; ++j) arr[j-1] = arr[j];
            --count;
            return true;
        }
    }
    return false;
}

Checklist / troubleshooting notes:

  • Only sort or display the active range (0..count-1); sorting/displaying unused slots shows garbage/defaults.
  • Comparator functions must return bool (not char) when used with std::sort.
  • Only call delete/delete[] for memory allocated with new/new[]; std::vector removes manual memory-management errors.
  • Input: use getline if names contain spaces; operator>> stops at whitespace.
  • Test by inserting a few records, removing one, then displaying the active set.

This addresses the main gaps raised by , , and : write a small, testable delete routine, track the number of valid records, and post the revised code for follow-up.

Recommended Answers

All 6 Replies

delete is a C++ keyword so use a different identifier other than
delete.

and also use the delete keyword to delete the arrays that you no
longer using. ( I mean inside that function ). Otherwise you will
get memory leaks.

commented: Indeed +36
void delete(student array[]) --> change function name
{
char s2=" ";
=" "; --> is it s1 declared here?
strcpy(s1,s2)-->> what its mean copying null to null??
" " -->>>what is this means?
}

First look at the discussion in this thread, which as of this writing is right below yours.

Then write some code, post it, and we'll go from there.

Also look at all of your other threads on the exact same topic on Daniweb, as well as on other forums. Sometimes people are answering on oher forums at the same time you have them open on Daniweb.

http://www.daniweb.com/forums/thread214624.html
http://www.daniweb.com/forums/thread214486.html
http://www.daniweb.com/forums/thread214440.html
http://www.daniweb.com/forums/thread214101.html
http://www.daniweb.com/forums/thread213808.html



http://www.cplusplus.com/forum/general/13911/

People spend time giving you help, then you tend not to bother responding and just start a new thread, often not taking the advice in the previous threads and just hoping someone will do the whole thing for you. Other people, not knowing about the other threads, waste time giving the same advice or close to it. And you STILL refuse to ask anything resembling a question in your posts. Just "it doesn't work".

Stop being selfish and wasting other people's time. Show you're willing some effort into this.

In this particular thread that VernonDozier posted above:

http://www.daniweb.com/forums/thread214101.html

look at post # 12 by Ancient Dragon where he gives you two possible explanations as to how you could go about creating a delete function. I'm not a betting man, but I'm willing to bet that if you look at either of those suggestions and then try to write your own code and post it here, these guys (myself included, if I can) will be willing to help.

-D

okay guys tnx for all ur comments i appreciate!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.