array and string.....

xfreebornx's Avatar, Join Date: Aug 2009
Newbie Member
please i need ideas on how i will put delete function in this program...?
tnx

Code:
#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;
}
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Duplicate of http://www.go4expert.com/showthread.php?t=19459

[[ Thread CLOSED ]]