I'm trying to write a code to take in strings of "Name Surname City" separated by spaces which are then sorted alphabetically first by city then surname then name. I have it as far as extracting the idividual elements of the string but don't know how to implement the bubblesort. Here's what I have so far:

#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 ten names and cities of residance and end with a space"<<"\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<<"\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";


  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 surname name
     cout<<"Name "<<i+1<<": "<<buffer1<<"\n";

         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++;
          }



    buffer2[start_pos]='\0';//this is the null char to terminate surname name

    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++;
          }



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

  }
    return 0;
}

Any help would be appreciated . Thanks

Modularation would help make your code more legible. Use a function for each task you want to accomplish. In this case I would do something like this:

#include <iostream>
#include <string>//because they are easier than using c-style arrays of characters
#include <fstream>//for file IO
#include <vector>//Again they are easier than using a normal array, and they are dynamic
using namespace std;
string readFile(string fname);//read the entire contents of a file
struct CityName//a structure to hold city, name, and surname
{
    string city;
    string name;
    string surname;
};
vector<CityName> parseString(string fcontents);//parse the fcontents string into an array of CityName structures
void sortCityName(vector<CityName> &vec);//sort the vector
void printCityName(CityName cn);//print the cityname
int main()
{
    string contents=readFile("myFileName.txt");//get the file contents
    vector<CityName> array=parseString(contents);//parse the file contents
    sortCityName(array);//sort the array
    for (int i=0; i<array.size(); ++i)//loop through the whole array
        printCityName(array[i]);
    return 0;
}

Would it be possible to create an array of arrays with ( (city) (name) (surname) ) and sort that?

It would be possible, but why use an array of arrays when you can use vector<string> and have the same effect. Basically vector<string> s is the same as char s[infinity][infinity] so why not use it? Also you may want to add a isGreater(string s1, string s2) function to help you in your sorting function.

ok, thanks, that was a great help. Much appreciated.

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.