#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;
		
		
		case 2:
		sort(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');
    }
}

void sort(student array[])
{
	if(array[i].name[0] < array[i+1].name[0])
     {
      student temp;
      strcpy (temp.name, array[i].name);
      strcpy (array[i].name, array[i+1].name);
      strcpy (array[i+1].name, temp.name);
     }
    
}

Recommended Answers

All 9 Replies

It might help if you actually asked a question instead of just wacking some code online and hope for the best.

okay the problem is that i want to sort for name and ID.

Compare and contrast (sorting wise) :

#include<iostream>
#include<vector>
#include<ctime> //for time.h
#include<algorithm>

using namespace std;

struct Info
{
	int id;
	char gender;

	Info() : id(0) , gender(' ') { }
	Info(int i, char g) :id(i), gender(g) { }
};

bool sortByID(const Info& a, const Info& b)
{
	return ( a.id < b.id ); // accending order
}
bool sortByName(const Info& a, const Info& b)
{
	return ( a.gender < b.gender); //accendign order
}
void myPrnt(const Info& a)
{
	
	cout<<"myHouse[i] = " << "id : "<<a.id<<"\t"<<"gender : "<<a.gender<<endl;
}
int main()
{
	srand( unsigned(time(0)) );
	const int MAX = 4;
	
	Info myHouse[MAX];
	
	//male,female,unknown
	char g[4] = "mfu"; //used below

	for(int i = 0; i < MAX; i++)
		myHouse[i] = Info(rand()%100,g[rand()%3]); //assign randomly

	cout<<"Original content : \n\n";
	std::for_each(myHouse,myHouse+MAX,myPrnt); //prnt

	cout<<"\n\nSorting by id number(accending order)  : \n\n";
	std::sort(myHouse,myHouse+MAX,sortByID); //prnt
	std::for_each(myHouse,myHouse+MAX,myPrnt); //prnt

	cout<<"\n\nSorting by gender(accending order) : \n\n";
	std::sort(myHouse,myHouse+MAX,sortByName);
	std::for_each(myHouse,myHouse+MAX,myPrnt); //prnt

	cout<<"\n\n";
}

I mentioned it in your last thread and Ancient Dragon mentioned it in your previous thread. Don't just post code with a title and no question. Doing that inevitably leads people to think that you simply want people to write it all for you and post it. Ain't gonna happen.

You have several threads on this topic, where you ask questions, people answer, then you don't respond to the suggestions with any new code, then you start a brand new thread with code that sort of incorporates some of the suggestions in the last thread, but in a way that suggests you haven't really understood the suggestions. The lack of any type of question in the new thread or a specific question requesting clarification in the old thread suggests that you are looking for someone to do it for you, not for someone to HELP you.

If you've never written a sort, drop this program temporarily, google "Bubble Sort" as Ancient Dragon mentioned earlier, and write a program that sorts an array of integers. Once you can sort an array of integers, come back to this program and try to sort an array of structs. The lack of a loop in your sort function, as well as the lack of an array length parameter passed to your Sort function suggests to me that you haven't googled "bubble sort" as AD suggested and gone over the sample code. The fact that you are still not using strcmp and instead just comparing the first letters also tells me that you didn't read some of my comments in your last thread. Or several of Ancient Dragon's and iamthwee's comments in the thread before that one.

commented: Well said +1

@firstperson tnx alot....
pls how can i link the 3 option to my sort in the main program?

@VernonDozier : wow. When someone writes that long of a
paragraphs you know he means it

@xfreebornx take a look :

int option = 0;
int quit = -1;
do
{ 
        cout <<"Welcome to student recording system"<<endl;
	cout <<"Please choose one of the following option"<<endl;
	cout <<"1. Insert new record"<<endl;
	cout <<"2. Sort record"<<endl;
    
       cin >> option;

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

        case 2: cout<<"you picked sorting\n". 
                     cout<<"Now sorting\n";
                    mySort(array, numberOfElements); //see way below
                    cout<<"Sorting done\n";
                    break;
    }

}while(option != quit)


//after end of main

bool mySort(int array[], int numberOfElements)
{
    if(numberOfElements == 0) return false;

   //Either create your own myCompareFunction, or use the ones that I provided in the previous post.
    std::sort(array,array+ numberOfElements, myCompareFunction); 

    return true;
}

@first person do u mean like this
sorry too much questions i'm new to c++

#include <iostream>
#include<vector>
#include<ctime> //for time.h
#include<algorithm>
using namespace std;

struct student
{
  int id;
  char name;
  char nationality;
  char gender;

student() : id(0) , gender(' ') { }
    student(int i, char g) :id(i), name(g) { }  
};

void insert(student array[]);  

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


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

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

void sort(const student& a)
{

    cout<<"Welcome to sorting function please do the following[i] = " ;
    cout<<"***************************************************";
    cout << "id : "<<a.id<<"\t"<<"name : "<<a.name<< "nationality: "<<a.nationality<<endl;
}

int main()
{   
    student array[100];


    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;


    int option = 0;
int quit = -1;
do
{ 
        cout <<"Welcome to student recording system"<<endl;
    cout <<"Please choose one of the following option"<<endl;
    cout <<"1. Insert new record"<<endl;
    cout <<"2. Sort record"<<endl;

       cin >> option;

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

        case 2: cout<<"you picked sorting\n". 
                     cout<<"Now sorting\n";
                    mySort(array, numberOfElements); //see way below
                    cout<<"Sorting done\n";
                    break;
    }

}while(option != quit)



     srand( unsigned(time(0)) );
    const int MAX = 4;

    student myHouse[MAX];

    //male,female,unknown
    char g[80] = "mfu"; //used below

    for(int i = 0; i < MAX; i++)
        myHouse[i] = student(rand()%100,g[rand()%3]); //assign randomly

    cout<<"Welcome to sorting function please do the following selection"<<endl;
    cout<<"***************************************************"<<endl;

    cout<<"* 1.Sort by student ID"<<endl;
    std::sort(myHouse,myHouse+MAX,sortByID); //prnt


    cout<<"* 2.Sort by student Name"<<endl;
    std::sort(myHouse,myHouse+MAX,sortByName);

    cout<<"* 3.Sort by Nationality"<<endl;
    std::sort(myHouse,myHouse+MAX,sortByNationality);
    cout<<"***************************************************"<<endl;
    cout<<"Selection: "<<endl;
    cout<<"\n\n";



  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');
    }
}
commented: 26 posts and still no code-tags -5

@VernonDozier : wow. When someone writes that long of a
paragraphs you know he means it

Yep, I definitely mean it.

@first person do u mean like this

Did you compile it? Does it run? What errors did you get? It looks like you just cut and pasted his code into yours and expected it to work.

Code tags: use them. You already know how since you used them on your first post.

Read this link.

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.