0

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;
            }
        } 
    }
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
VRIL
  • 9
  • 1
    With [`std::sort`](https://en.cppreference.com/w/cpp/algorithm/sort) using a suitable comparison function? – Some programmer dude Jul 21 '20 at 14:38
  • 5
    Maybe try using C++ `std::string`s instead of `char` arrays, and then `>` and `<` will work as expected, without much effort. – Sam Varshavchik Jul 21 '20 at 14:39
  • 3
    You should probably take some time to learn about [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string) and [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) as well. A [decent C++ book or two](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) could also be helpful. – Some programmer dude Jul 21 '20 at 14:39
  • 1
    Store them in a `std::map`, with the name as the key and the number as the value. – Mansoor Jul 21 '20 at 14:41
  • 3
    Also, `temp[50] = cont[i].name[50];` doesn't do what you think it does. This results in memory corruption. You should stop trying to write C++ programs by randomly trying things until they compile. This is unlikely to result in a correct program. You were trying to probably copy one array to another, but this is not how you do that. If you don't know how to do something in C++, you should consult your C++ textbook, instead of trying to guess. Your guess will likely be wrong, even if it compiles. C++ is the most complicated general purpose programming language in use today, for a good reason. – Sam Varshavchik Jul 21 '20 at 14:44
  • Let's not forget that `std::cin` won't work properly if they are entering full names. `std::getline()` is better. – sweenish Jul 21 '20 at 14:56
  • @Someprogrammerdude can you give an example of a comparison function? i'm kinda new and i have no idea how to use the sort function properly. – VRIL Jul 22 '20 at 08:21

1 Answers1

1

Just one of all possible approaches: You can use the std::sort with a comparison function object. Here, it is a lambda function.

std::sort(std::begin(cont), std::end(cont), [&](const contact& conA, const contact& conB){
    std::string A(conA.name), B(conB.name);
    return A < B;
});
poyea
  • 113
  • 2
  • 5
  • 1
    You can use `std::begin(cont), std::end(cont)` for iterators instead of `sizeof`. The advantage being that it will not compile if `cont` is changed to a pointer later on. While the manual sizeof computation will fail silently, but badly. – bitmask Jul 21 '20 at 15:38