Hi!
I'm writing a program in C++ where the user is asked to enter a list of ten names and cities of residence in the format <firstname> <Surname> <city>.

I then have to sort the list by city and then in each city group alphabetically by name, using Bubblesort.

I'm able to extract each of the three components from the strings, but now I'm really lost on how to implement the Bubblesort correctly. All of the notes I have seen are for sorting numbers, not letters. Can anyone help me or offer suggestions as to how to sort the names?

This is my code:

#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;

int main()
{
 char name[10][80];
 char buffer1 [80];
 char buffer2 [80];
 char buffer3 [80];
 int i,j,k,q,word_size,start_pos,end_pos,name_size,city_start,city_end,city_length;


 cout<<"Enter 10 names and cities of residence: "<<"\n";
 gets( name[0]);
 gets( name[1]);
 gets( name[2]);
 gets( name[3]);
 gets( name[4]);
 gets( name[5]);
 gets( name[6]);
 gets( name[7]);
 gets( name[8]);
 gets( name[9]);

 cout<<"***Initialise check***"<<"\n";


cout<<"\n";
cout<<"\n";
cout<< name[0]<< "\n";
cout<< name[1]<< "\n";
cout<< name[2]<< "\n";
cout<< name[3]<< "\n";
cout<< name[4]<< "\n";
cout<< name[5]<< "\n";
cout<< name[6]<< "\n";
cout<< name[7]<< "\n";
cout<< name[8]<< "\n";
cout<< name[9]<< "\n"<<"\n";

cout<< "***Check Complete***"<<"\n";

  for (i=0; i<=9; i++)
  { j=0;

   while (isspace  (name[i][j]) )

     {j=j+1;

     }


         k=j;
         while(!isspace(name[i][k])) k=k+1; //advances through the first name
         name_size = k-j;

         for (q=0; q<=name_size; q++)
          {
            // this generates the name
            buffer1[q] = name[i][j];
            j++;

          }

     buffer1[j]='\0';//this is the null char to terminate first name name
     cout<<"Name "<<i+1<<": "<<buffer1<<"\n";

   if (j<80){
         while(isspace(name[i][k])) k=k+1;   // advances through the next block of spaces
         start_pos=k;
         while(!isspace(name[i][k])) k=k+1;// advances through the surname
         end_pos=k;

         word_size = end_pos - start_pos;
   }

     // check word size of first char


     for (q=0; q<=word_size; q++)
          {
            // this generates the surname
            buffer2[q] = name[i][start_pos];
            start_pos++;

          }
           //this is the null char to terminate surname name
    buffer2[start_pos]='\0';
    cout << "Surname "<<i+1<<": "<<buffer2 << "\n";


    while(isspace(name[i][k])) k++;

    city_start=k;
    while(!isspace(name[i][k])) k++;

    city_end=k;
    city_length=city_end-city_start;


    for (q=0; q<=city_length; q++)
          {
            // this generates the city
            buffer3[q] = name[i][city_start];
            city_start++;

          }

          //this is the null char to terminate surname name
    buffer3[city_start]='\0';
    cout<< "City " << i+1 <<": "<< buffer3 <<"\n";
    cout<< "\n" << "\n";

  }

    return 0;
}

Recommended Answers

All 9 Replies

To compare C style strings you would use strcmp(), or a related function, rather than the < or the > or the == operator like you would for numerical values.

If you are alllowed to use STL string objects, then the STL string class has the <, > and == operators overloaded for you.

To compare C style strings you would use strcmp(), or a related function, rather than the < or the > or the == operator like you would for numerical values.

If you are alllowed to use STL string objects, then the STL string class has the <, > and == operators overloaded for you.

Thanks for your post! Are you saying I should worry about using strcmp and the STL string class rather than Bubblesort so?? :)

No. You need to compare items to be sorted in Bubble Sort. You can't use the math comparison operators to compare C style strings. You need to use strcmp(), or related functions. You use C style strings in the program you wrote. Because of the cumbersome nature of strcmp() you could potentially streamline things somewhat if you were allowed to use STL string objects instead of C styl strings, as the STL class has the math operators overloaded for you so you don't need to use the cumbersome strcmp(), or related, function(s).

No. You need to compare items to be sorted in Bubble Sort. You can't use the math comparison operators to compare C style strings. You need to use strcmp(), or related functions. You use C style strings in the program you wrote. Because of the cumbersome nature of strcmp() you could potentially streamline things somewhat if you were allowed to use STL string objects instead of C styl strings, as the STL class has the math operators overloaded for you so you don't need to use the cumbersome strcmp(), or related, function(s).

Thank you for your reply. I'm afraid, I don't fully understand what you're suggesting I do. I'm very new to programming, and have very little experience with it! Would you be able to illustrate or explain you suggestion code-wise please? Or anyone else for that matter!!! :)

Thank you for your reply. I'm afraid, I don't fully understand what you're suggesting I do. I'm very new to programming, and have very little experience with it! Would you be able to illustrate or explain you suggestion code-wise please? Or anyone else for that matter!!! :)

I've been trying to implement the Bubblesort code, this is what I have so far:

char temp;

for (i=0;i<10;i++)
{
for (j=i+1;j<80;j++)
{
if (strcmp(strbuffer3,buffer3[j])>0)
{
strcpy(temp,buffer3);
strcpy(buffer3,buffer3[j]);
strcpy(buffer3[j],temp);
}
}
}cout<< "The sorted Names are: "<< endl;
for (i=0;i<10;i++)
{
cout<<"City: " << i+1 <<"\n ",i,str);
}

Not too bad. However, your boundaries in the for loops are off a bit. Since you will be comparing elements at i and i + i you want in to be no larger than the next to last element in the array. If there are 10 elements the last valid element is 9 so you don't want i to be more than eight. I general if array has n elements then you want i to be less than n - 1. j however, can be the last element the array or less than n if there are n elements in the array.

In addition, where did strbuffer come from?

Also, remember to use code tags when posting code to this board!

Students are NOT allowed to post on forums,
Damian Dalton.

>>Students are NOT allowed to post on forums,

Where did you get that idea? There are guidelines to be followed to ensure a learning process, particularly when the post has to do with homework, but we are all students as we all have something to learn.

Students are NOT allowed to post on forums,
Damian Dalton.

so sorry but this is NOT damian dalton....again my friend was messing and best of luck with the assignment

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.