How can I sort the entered contacts alphabetically and display them? I've tried a few methods but no success so far.
using namespace std;
struct contact{
char name[50];
int number;
};
contact cont[50];
void add_new_contact(){
int getNum;
for (int i=0;i<50;i++){
cout << "Enter contact name: ";
cin >> cont[i].name;
cout << "Enter contact number: ";
cin >> cont[i].number;
cout << "Would you like to add another contact?\n";
cout << "1. Yes\n";
cout << "2. No\n";
cout << "Choice: ";
cin >> getNum;
if (getNum == 2)
break;
else
continue;
}
}
void sort_in_alphabetical_order(){
int i,j;
char temp[50];
for (i = 0; i < 50; i++) {
if (cont[i].name[0] > cont[i+1].name[0]) {
temp[50] = cont[i].name[50];
cont[i].name[50] = cont[i+1].name[50];
cont[i+1].name[50] = temp[50];
cout << cont[i+1].name << endl;
}
}
}