View Single Post
  #3 (permalink)  
Old May 20th, 2008, 04:09 PM
code_lover code_lover is offline
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default

Dear ciderpunx, thankyou very much for your useful tips. You have really added to my knowledge. I have the complete code now to sort 10 names wrt their 1st character.Now can you please tell me how to modularise this code by using user-defined functions. I'm a newbie in C++ so bear with me.


#include <iostream.h>
#include <string.h>
#include <conio.h>





main()
{
      char name[10][25];
      char temp[25];

      /*How to write and use this code through my own function*/
      for(int i=0;i<=9;i++)
      {
              cout<<"\nEnter name"<<i+1<<": ";
              cin>>n[i];
      }
      /******************************************/
      cout<<"The sorted names are: "<<endl;

       /*How to write and use this code through my own function*/
      for (int iteration=0;iteration<=9;iteration++)
      {
          for(int k=0;k<=8;k++)
          {
              if (tolower(name[k][0])>tolower(name[k+1][0]))
              {
              strcpy(temp,name[k]);
              strcpy(name[k],name[k+1]);
              strcpy(name[k+1],temp);
              }
          }

      }

    for(int l=0;l<=9;l++)
    {
            cout<<name[l]<<endl;
    }
     /******************************************/
getch();

}

Thanks!

MAXOOD!

Life is an endless journey towards perfection
Reply With Quote
The Following User Says Thank You to code_lover For This Useful Post: