sort first name by alphabets

Hi, im currently facing the sort first name according to the alphabets at Edit() part. i want my name show and sort by first name according to alphabets. need somebody to teach me how to fix this?? this is part of my code.


struct Contact
{
char first[20]; /*First Name*/
char last[30]; /*Last Name*/
};

class PhoneBook
{
private:
Contact * contact;
void ImportData();
void ExportData();
void WriteToFile(int index);
void DeleteFile();
void ReadFromFile();
public:
PhoneBook();
void Add();
void Edit();
bool NameIsExist(char keyword[20], char keyword2[30]);
bool MobileIsExist(char keyword[10]);
bool SearchContact(char keyword[60]);
int currentIndex;
};

PhoneBook::PhoneBook()
{
contact = new Contact[MAX_CONTACTS];
currentIndex = 0;
ReadFromFile();
}

void PhoneBook::DeleteFile()
{
remove("D:\\phonebook.txt");
}


void PhoneBook::ReadFromFile()
{
ifstream studentFile("D:\\phonebook.txt");
currentIndex = -1;
if (studentFile.is_open()) { //checks to see if file is good and that it opened correctly

while(!studentFile.eof()) { //while we are not at the end of the file
string mstring;
getline(studentFile, mstring); // Saves the line in myString

char *myString = (char*)mstring.c_str();

if(mstring!="")
{
currentIndex++;

int i = 0;
int j = 0;
while(myString[i] != ';')
{
contact[currentIndex].first[j] = myString[i];
j++;
i++;
}

contact[currentIndex].first[j] = '\0';

i++;
j = 0;
while(myString[i] != ';')
{
contact[currentIndex].last[j] = myString[i];
j++;
i++;
}
contact[currentIndex].last[j] = '\0';
i++;
j = 0;


}
}
}
currentIndex++;
}

void PhoneBook::WriteToFile(int index)
{
ofstream myfile;
myfile.open ("D:\\phonebook.txt", fstream::in | fstream::out | fstream::app);

string out = "";
out.append(contact[index].first);
out.append(";");
out.append(contact[index].last);
out.append(";");

myfile << out;
myfile.close();
}

void PhoneBook::Add()
{

bool validInput = true;

dataentry:
do
{
if( !validInput )
cout << "Only alphabet is available! Please retype again!\n" << endl ;
cout<< "1.First Name : " ;
cin.ignore();
cin.getline(contact[currentIndex].first,20);

if( !ISALPHABETIC(contact[currentIndex].first) )
validInput = false;
else
validInput = true;
} while( ! validInput );

do
{
if( !validInput )
cout << "Only alphabet is available! Please retype again!\n" << endl ;
cout << "2.Last Name : " ;
cin.getline(contact[currentIndex].last,30);

if( !ISALPHABETIC(contact[currentIndex].last) )
validInput = false;
else
validInput = true;

if(validInput)
{
if(!NameIsExist(contact[currentIndex].first, contact[currentIndex].last))
{
validInput = true;
}
else
{
cout << "Name already exist! Please retype!\n" << endl;
goto dataentry;
}
}
} while( ! validInput );

cout << endl;
cout << "DONE!" << endl;

//save to file
WriteToFile(currentIndex);
currentIndex++;

}


void PhoneBook::Edit()
{

cout << "Select a consumer to edit:" << endl ;
for(int i=0; i < currentIndex; i++)
{
if(contact[i].first[0] != '\\')
{
cout << (i+1) << ". " << contact[i].first << " " << contact[i].last << endl;
}
}
cout << endl;


int index;
while(!(cin>>index)||index<0){

cin.clear();

while(cin.get()!='\n'){
continue;
cin>>index;
}
cout<<"Only numbers allow! Please choose again!"<<endl;
}
index = index -1;



if(index <= currentIndex)
{
if(contact[index].first[0] != '\\')
{
cout << "Edit contact: [" << (index+1) << ". " << contact[index].first << " " << contact[index-1].last << "]"<< endl;

int field;

cout << "Select field to edit:" << endl;
cout << "1. First Name" << endl;
cout << "2. Last Name" << endl;
cout << "3. Company" << endl;
cout << "4. Mobile Number" << endl;
cout << "5. Office Number" << endl;
cout << "6. Office Address" << endl;
cout << "7. Email" << endl;


while(!(cin>>field)||field<0){
cin.clear();
while(cin.get()!='\n'){

continue;
cin>>field;
}
cout<<"Only numbers allow! Please choose again!"<<endl;
}

bool validInput = true;

switch(field)
{
case 1:
cout << "1. First Name : ";
do
{
if( !validInput )
{
cout << "Only alphabet is available! Please retype again!" << endl ;
cout<< "1.First Name : " ;
}
cin.ignore();
cin.getline(contact[index].first,20);

if( !ISALPHABETIC(contact[index].first) )
validInput = false;
else
validInput = true;
} while( ! validInput );
break;

case 2:
cout << "2. Last Name : ";
do
{
if( !validInput )
{
cout << "Only alphabets is available! Please retype again!" << endl ;
cout<< "2. Last Name : " ;
}
cin.ignore();
cin.getline(contact[index].last,30);;

if( !ISALPHABETIC(contact[index].last) )
validInput = false;
else
validInput = true;
} while( ! validInput );
break;
}

// save to file
DeleteFile();
for (int i = 0; i < currentIndex; i++)
{
if(contact[i].first[0] != '\\')
{
WriteToFile(i);
}
}
}
}

else
{
cout << "Record not available. Please choose another.\n";
}
}
Last edited on
First and foremost, use code tags. Most of us won't read a post if the code is not properly formatted using code tags.

Second, what does Edit() have to do with sorting? I just don't get it. If what you want is to keep an array sorted after the sort key is potentially changed (in Edit()), then I guess all you have to do is either re-insert the element (more complex but more efficient), or simply call your sort() routine (I am guessing you have a sort routine somewhere; I didn't read the code because it is not formatted).

If what you need is a sorting algorithm, you can check Wikipedia for all different available methods, then pick one and code it. Alternatively, if you are allowed, you could use std::sort(), available in the <algorithm> header file.
Topic archived. No new replies allowed.