dear all!!!
here is my code. when I run it "enter name and enter city "appears together.
I dont understand why. please help me figure it out.
thank you.

#include <iostream>
#include <string>

using namespace std;

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

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

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

void prompt(string name[7], char city[7])
{
    for(int i=0;i<=6;i++)
    {
        cout << "Please enter name: ";
        cin >> name[i];
        cout << "Please enter city: ";
        cin >> city[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 output(string name[7], char city[7], int index[7])
{
    int i;
    cout << endl;

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

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

    //system("pause");
}

Recommended Answers

All 4 Replies

Are you trying to read the whole name or one character of the name?

dear thines01
I need to put in the whole name.

OK, so you're trying to read in seven names and seven cities then sort them?

Why dont you use string for both name and city? Try changing it to string also

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.