0

hi everyone!!!
I am trying to write a code for list of names to be sorted in alphabetical order.
can anybody please tell me what I am doing wrong.
and one more thing , is it possible to bubble sort a names in unspecified number of entries? and if it is possible how will the program know where is the end of the list?
I was also wondering if I could do bubble sort with strings?
many thanks in advance.
here is what I have got so far

include <iostream>

using namespace std;

int main()
{
char name[3][30];
int i, j;

for(i=0;i<=2;i++)
{
    cout << "Please enter name: ";
    cin >> name[i];
}

for(i=0;i<=1;i++)
{
    for(j=i+1;j<=2;j++)
    {
        char temp;// problems with this one , i have changed int to char

        if(name[i] > name[j])
        {
            temp = name[i];// I have some problems here
            name[i] = name[j];
            name[j] = temp;
        }
    }
}

for(i=0;i<=2;i++)
{
    cout << endl <<  name[i] << endl;
}

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

//system("pause");

}

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Lerner
0

declare temp as a char array of size 30, just like each name[i].

You can't use <,>, or = on C style strings (you can on STL string objects). Use strcmp() and strcpy() as needed. They should be in the cstring header file, if I remember correctly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.