#include <iostream>
using namespace std;

struct student
{
  int ID;
  char name[80];
  char nationality[80];
  char gender[2];
};

void insert(student array[]);  
void Sort(student array[]);

int main()
{	
	student array[100];
	int answer;
	
	cout <<"Welcome to student recording system"<<endl;
	cout <<"Please choose one of the following option"<<endl;
	cout <<"1.Insert new record"<<endl;
	cout <<"2.Delete record"<<endl;
	cout <<"3.Sort record"<<endl;
	cout <<"4.Display record"<<endl;
	
	
	cin >>answer;
	switch(answer)
	{
		case 1:
		insert(array);
		break;
		
	
     }
  
		 
  Sort(array);
  insert(array);
  return 0;
}

void insert(student array[])
{
  for(int i=0;i<100;i++)
  {

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

Dani AI

Generated

Quick summary and practical fixes for the original program: the current code reads a fixed 100 records with no menu loop, has no display method or a reliable count of how many entries exist, and uses raw C strings which invite buffer overruns and make swapping tedious. already flagged the missing loop; correctly pushed for a Display() before trying deletes or sorts. Replace the fixed-array/C-string design with a simple container and safer types, then implement a clear workflow: input (with validation) → display → sort → remove duplicates → repeat.

A minimal, modern structure (conceptual) looks like this:

struct Student {
    int ID;
    std::string name;
    std::string nationality;
    char gender; // 'M' / 'F' or use an enum
};

std::vector<Student> students;

Sort by name or ID with the STL sorter (you only need to change the comparator), then remove duplicates after sorting by comparing the key you care about (name or ID):

std::sort(students.begin(), students.end(),
          [](auto const& a, auto const& b){ return a.name < b.name; });

students.erase(std::unique(students.begin(), students.end(),
                           [](auto const& a, auto const& b){ return a.name == b.name; }),
               students.end());

Alternative: prevent duplicates at insertion using a hash set of seen names/IDs. That is often simpler when you want to reject duplicates immediately instead of cleaning the list later.

Practical tips and cautions: use getline for full names (not operator>>), keep a vector size or an explicit count so your loops know how many entries exist, implement a simple menu loop in main to call Insert/Display/Sort/Delete, and test on a small dataset first. For case-insensitive comparisons, normalize strings (tolower) before comparing. Change gender[2] to char or an enum to avoid wasted space and ambiguity. These changes make sorting and duplicate removal robust and much easier to implement and test than manual C-string swapping.

Recommended Answers

All 13 Replies

One, mark your old threads dealing with this question solved so people don't waste time commenting on them there instead of here.

Two, what's the question?

okay thanks...
here are the question
I want to create a sort that can sort student name or id
secondly delete names that are entered twice...
thanks

There is no loop in your main function, so if someone wants to see the changes, he can't.

You could sort the names by making a a loop with i as the index of the array, then just look at array and array [i+1] and see which one should come earlier and switch their places if needed.
Do this a few times, and your list should be sorted.
And btw, use a bool for gender (uses less memory and easier to use)

i want to only base on names or id.....

then compare the first character of the name variable of the array

if(array[i].name[0] < array[i+1].name[0]){
// the switching stuff
}

i don't understand what u mean by the switching stuff??

I'll try to explain:

you need to see which object comes first in the alphabet
So you compare the first letters of the names, then you determine which one should be first.

If the name of object1 is bbbb and object2 is aaaa then you need to switch the two.

You make a temporary object
student temp.
You assign object2 to temp
you assign object1 to object2
then you assign temp to object1

so that object1 and object2 switch their places.

I hope this explains atleast a little

do u mean something like this?

if(array[i].name[0] < array[i+1].name[0])
 {
     student temp.
     name.temp
     ID.name
 }

then compare the first character of the name variable of the array

if(array[i].name[0] < array[i+1].name[0]){
// the switching stuff
}

No need to compare a character at a time. There's a function called strcmp for this very purpose. It'll compare the entire string for you:


do u mean something like this?

xfreebornx, put code in code tags, not quote tags. As you can see, quotes disappear when replied to. Code does not, if you put it in code tags.

if(array[i].name[0] < array[i+1].name[0])
{
student temp.
name.temp
ID.name
}

This makes no sense and won't compile. Again, don't compare the characters. Do this instead:

if (strcmp (array[i].name, array[i+1].name) < 0)
{
    // set up a temp object and swap
}

To swap, you need to copy ID, name, nationality, and gender:

student temp;
strcpy (temp.name, array[i].name);
strcpy (array[i].name, array[i+1].name);
strcpy (array[i+1].name, temp.name);

Do this for the other parts of student. ID is an int, so don't use strcpy there, obviously.

okay tnx i appreciate

hey how about the delete how can i delete data that hav been entered twice?

hey how about the delete how can i delete data that hav been entered twice?

You need to put some revised code up at this point. Get the sorting part right and post follow-up questions on what has been answered so far. Worry about delete after sort is working and after you've posted a Display () method. You should have a Display () method before starting the sort and the input work. Otherwise, how can you test anything?

how to delete all names starting with agiven charecter in linked list please i need the answer quickly!

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.