How can i write a C++ program which prompts the user to enter names of five countries , stores them in a 2D array,sorts them alphabetical order and prints them on screen.
im unable to make it..plz help

#include <iostream>
using namespace std;
int main()

    cout<<"Enter tha names of five countries."<<endl;
    int i,j;

char arr[5][20];

for(int k=0;k<=4;k++)
{
    cin>>arr[k];
}

for( i=0;i<=4;i++)
{
    for( j=0;arr[i][j]!='\0';j++)
    {
        cout<<arr[i][j];
    }
    cout<<endl;
}

cout<<"The name of these countries in alphabetical order :"<<endl;

int x[5];

for(i=0;i<=4;i++)
{
    x[i]=arr[i][0]-64;
    cout<<x[i]<<endl;
}
const int size =5;
int pass=size-1;
int temp;
for(k=0;k<pass;k++)
{   
    for(j=0;j<pass;j++)
    {
        if(x[j]>x[j+1])
        {   temp=x[j];
            x[j]=x[j+1];
            x[j+1]=temp;
        }
    }

}


for( i=0;i<=size;i++)
{
    for( j=0;j<=15;j++)
    {

        arr[i][j]=x[i+64];
        cout<<arr[i][j];
    }
    cout<<endl;
}

    return 0;
}

Start by making sure that your driver is working the way you want using a standard sorting library. Here's one that uses qsort in C-style since you're into C-style strings:

#include <cstdlib>  // std::qsort is here
#include <cstring>
#include <iostream>

using namespace std;

namespace {
    const int COUNTRY_MAX = 5;
    const int COUNTRY_NAME_MAX = 255;

    int compare(const void *a, const void *b)
    {
        return strcmp((const char*)a, (const char*)b);
    }
}

int main()
{
    char countries[COUNTRY_MAX][COUNTRY_NAME_MAX] = {0};

    cout << "Enter " << COUNTRY_MAX << " countries.\n";

    for (int i = 0; i < COUNTRY_MAX; i++) {
        cout << i + 1 << ": ";
        cin.getline(countries[i], COUNTRY_MAX);
    }

    qsort(countries, COUNTRY_MAX, COUNTRY_NAME_MAX, compare);

    for (int i = 0; i < COUNTRY_MAX; i++) {
        cout << countries[i] << '\n';
    }
}

If this is sufficient, you're done! Otherwise, you might be asked to write your own sorting function, which is relatively straightforward and also a good learning exercise.

Keep in mind that C-style strings are arrays and do not support direct assignment. You'd need to use something like strcpy (still thinking in C mode) to copy the content of the arrays.

On a side note, 20 is too small for country names. IIRC the longest official country name pushes 70 characters, and unofficial names push 200. The ideal would be to use std::string and let each object size itself properly so you don't need to worry about such things. An added benefit is that std::string supports copying with the assignment operator and no special consideration is needed with your swap logic.

thanks

its not running properly....its truncating the string plus not working for few coutries names

Post your code and the examples that aren't working the way you think they should.

FOR country name Pakistan..the output console does not get the next four countries input

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.