Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi everyone, this is assignment question And I am really struggling to solve this I have been stuck for hours. Can you please help me solve this?

Write a C++ program to prompt the user to enter a number of names, then print out all the names in alphabetical order of the last names. If the last names are the same, then the first names are compared. A sample run is attached as follows (the text in bold face is user input):

How many names do you want to enter? 5
Enter name #1: Bill Gates
Enter name #2: Michael Jackson
Enter name #3: Tony Blair
Enter name #4: Janet Jackson
Enter name #5: Oprah Winfrey
The names in order are:
Blair, Tony
Gates, Bill
Jackson, Janet
Jackson, Michael
Winfrey, Oprah

The strings we are using in this program are C-strings. You cannot use C++ <string>. And, you cannot use any <cstring> library function other than the overloaded operator << and operator >>

What I have tried:

<pre>char** name_list1(int n)
{
    char** array1 = new char*[n];
    for(int i =0; i < n; i++)
    {
        array1[i] = new char[20];
    }

    //char names[n][20];
    for(int i = 0; i < n; i++)
    {
        cout << "Enter name " << i << ": ";
        cin.getline(array1[i], 20);
    }
    return array1;
}


int Number_of_names;
    cout << "How many names do you want to enter? ";
    cin >> Number_of_names;

    char** array2 = new char*[Number_of_names];
    for(int i =0; i < Number_of_names; i++)
    {
        array2[i] = new char[20];
    }
    array2 = name_list1(Number_of_names);


return 0;
}
Posted
Updated 23-Aug-21 7:02am
Comments
Richard MacCutchan 5-Aug-21 5:25am    
You now need to write a sort function that takes the input array and constructs a new sorted array, based on the rules given above.
Subhranil Dey 5-Aug-21 5:41am    
hey Richard, thanks for your reply. There is a slight problem I'm facing. when I run the program the position for the first name does not take any input and goes straight to secound.
CPallini 5-Aug-21 5:57am    
use cin.ignore. See, for instance
https://newbedev.com/when-and-why-do-i-need-to-use-cin-ignore-in-c
Subhranil Dey 5-Aug-21 6:28am    
Thank You so much! although we were not taught this in class and we are only supposed to use material from class but it worked. I will post my other two functions for the sorting, please take a look at it if You get the time
CPallini 5-Aug-21 6:34am    
You are welcome.

1 solution

If is not a constraint, use std::set

C++
#include<set>
....

    std::set<string> s
    for(int i = 0; i < n; i++)
    {
        cout << "Enter name " << i << ": ";
        std::string name;
        std::getline (std::cin,  name);
        s.insert(name);
    }

    //the values will be ordered
    for(std::set<string>::iterator it = s.begin(); it != s.end(); it++)
        cout<< *it<< endl;
    //or in C++ 14 you can do
    for(auto n: s)
        cout<< *n<< endl;
    //iterate in reverse order
    for(std::set<string>::reverse_iterator it = s.rbegin(); it != s.rend(); it++)
        cout<< *it<< endl;

The values in set are unique. You can use multiset if you don't require elements to be unique.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900