hello good people
I am doing indexsort of name and surename.
i have some error with index in my sort2 fuction.
I can not figure outwhat is wrong.
can somebody give me a hint about what to do to fix it.
I was also wondering if I can add a list of cities as well.
thanking you in advance.

#include <iostream>
#include <string>

using namespace std;

void prompt(string name[7], string surename[7]);
void init(int index[7]);
void sort(string name[7], int index[7]);
void sort2(string surename[7], int[7]);
void output(string surename[7], string name[7], int index[7]);

int main()
{
    string name[7];
    string surename[7];
    int index[7];

    prompt(name, surename);
    init(index);
    sort(name, index);
    sort(surename,index);
    output(surename, name, index);
}

void prompt(string name[7], string surename[7])
{
    for(int i=0;i<=6;i++)
    {
        cout << "Please enter name: ";
        cin >> name[i];
        cout << "Please enter surename: ";
        cin >> surename[i];
    }
}

void init(int index[7])
{
    for(int i=0;i<=6;i++)
    {
        index[i]=i;
    }

}

void sort(string name[7], int index[7])
{
    int i, j;
    for(i=0;i<=5;i++)
    {
        for(j=i+1;j<=6;j++)
        {
            int temp;

            if(name[index[i]] > name[index[j]])
            {
                temp = index[i];
                index[i] = index[j];
                index[j] = temp;
            }
        }
    }
}
void sort2(string surename[7], int[7])
{
    int d, t;
    for(d=0;d<=5;d++)
    {
        for(t=d+1;t<=6;d++)
        {
            int temp;

            if(surename[index[d]] > surename[index[t]])
            {
                temp = index[d];
                index[d] = index[t];
                index[t] = temp;
            }
        }
    }
}
void output(string surename[7], string name[7], int index[7])

{
    int i;
    cout << endl;

    for(i=0;i<=6;i++)
    {
        cout << surename[index[i]] << "        "
            << name[index[i]] << endl;
    }

    cin.ignore();
    cin.get();

    //system("pause");
}

If sort works then you should have no trouble with sort2. In fact please delete sort2. The thing you do not seem to realize is that sort and sort2 are EXACTLY the same. Just changing the name of the parameters does not change the function. Here is an example:

void addOne(int &numberOne){numberOne++;}
int main()
{
    int numberOne;
    int numberTwo;
    cin>>numberOne>>numberTwo;
    addOne(numberOne);//okay, we will add one to number one
    addOne(numberTwo);//this is still okay because numberTwo gets sent to the addOne function, which gives it the name numberOne, but just for the duration of the function
    cout<<numberOne<<numberTwo<<endl;
    return 0;
}
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.