How to Sort Students Names Records in Ascending Orders in C++

This is the beginning level program in C++ about Name Sorting. The program takes the names and grades of students from the user and sorts the names in ascending order.

Source Code 

#include <iostream>
#include<iomanip>
 #include<stdlib.h>
 #include<conio.h>
#include <algorithm>
using namespace std;
struct student
{
 string name;
 int id;
 char grade[3];
 };
//global variables
student stdInstance[50];
int size = 1;
//functions
void welcomeScrean(void);
void displayAllStudents(void);
void sortAndDisplayAllStudents(void);
void getStudentsWithGrade(void);

int main()
{
 system ("color F0");
 welcomeScrean();
 int i=0,j=0;
 cout<<"\t  How many students do you want to enter  "    <<endl
  <<"\t  ======================================"      <<endl
  <<"\t  You Endered : ";
 cin>>j;
 for(i;i<j;i++)
 {
  getStudentsWithGrade();
 }
 
 displayAllStudents();
 sortAndDisplayAllStudents();
 return 0;
 }
void welcomeScrean(void){
  cout<<"\a"
  <<"\t*******************************************************"<<endl
  <<"\t**                                                   **"<<endl
  <<"\t**                   Welcome To                      **"<<endl
  <<"\t**                                                   **"<<endl
  <<"\t**       Student Name Sorting and Data Entry         **"<<endl
  <<"\t**                                                   **"<<endl
  <<"\t**                    Program                        **"<<endl
  <<"\t**                                                   **"<<endl
  <<"\t*******************************************************"<<endl
  <<endl
  <<endl;
 cout<<"\t\t\t\n";
}
void displayAllStudents(void){
 cout<<endl           
  <<"\t  Students added with following data"          <<endl
  <<"\t  ======================================"      <<endl
  <<endl;
 
 for(int n=1;n<size;n++)
 {
  cout<<"\t  "<<stdInstance[n].name<<endl;
 }
}
void sortAndDisplayAllStudents(void){
 string temp[size]; 

 for ( int h = 0 ; h<size ; h++ )               
    {
        temp[h]=stdInstance[h].name;
    }
 sort(temp, temp + size);
 cout<< "\t  Sorted names are"<<endl;
 for(int m=1;m<size;m++)
 {
  cout<<endl<< "\t  "<<temp[m]<<endl;
 }
}
void getStudentsWithGrade(void){
 //system("CLS");
  cout<<endl
  <<"\a"
  <<"\t  *******************************************************"<<endl
  <<"\t  **             Adding Student No # "<<size<<"                 **"<<endl
  <<"\t  *******************************************************"<<endl;

 cout<<endl
  <<    "\n\tEnter Name : ";

 cin>>stdInstance[size].name;
 
 cout<<    "\n\tEnter Grade TERMINATONG at \".\": ";
 cin.getline(stdInstance[size].grade,3,'.');//taking grade
 
 stdInstance[size].id = size;//settind customer id

 size++;
 cout<<endl           
  <<"\t  Press Enter to continue . . ."          <<endl;
 getch();
}

Output of the Program

how to sort names in C | name sorting in c


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment