swapping and sorting text

This is a discussion on swapping and sorting text within the C++ Programming forums, part of the General Programming Boards category; Hi all, Here is my problem. I am just starting to program with C++ and i'm trying to figure out ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Question swapping and sorting text

    Hi all,

    Here is my problem. I am just starting to program with C++ and i'm trying to figure out the best way to hold a character string compare it and then swap it to get an ascending order listing. So far I have this much:
    Code:
    for (int i = 0; i < arraysize; i++){
    
    	cout << "Please enter town name: " << endl;
    
    	cin.getline(name, arraysize, '\n');
    	strcpy(town, name);
    	town[strlen(name)] = '\0';
    
    	strcmp(town, name);
    	hold = town;		
    
                    if (town[i] == '\0')
    	       return 0;
    	return(town[i] - hold[i]);
    
    	cout << town << '\n';
    	cout << endl;
    I am a beginner learning so any help would be greatly appreciated.

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i'm trying to figure out the best way to hold a character string compare it and then swap it to get an ascending order listing
    Code:
    #include <iostream>
    #include <algorithm>
    #include <string>
    
    const int N_NAMES = 5;
    
    int main()
    {
      std::string names[N_NAMES];
    
      for ( int i = 0; i < N_NAMES; ++i ) {
        std::cout<<"Enter a name: ";
    
        std::getline ( std::cin, names[i] );
      }
    
      std::sort ( names, names + N_NAMES );
    
      for ( int i = 0; i < N_NAMES; ++i )
        std::cout<< names[i] <<std::endl;
    
      std::cin.get();
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting a doubly linked (linux-style) list
    By smoking81 in forum C Programming
    Replies: 4
    Last Post: 10-01-2008, 02:02 AM
  2. Bubble Sorting
    By yukapuka in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2008, 10:44 PM
  3. sorting a linked list
    By sillyman in forum C Programming
    Replies: 4
    Last Post: 04-30-2008, 10:39 AM
  4. Sorting Linked List Problem
    By chriscolden in forum C Programming
    Replies: 8
    Last Post: 01-17-2006, 10:46 AM
  5. Swapping Pointers & Arrays
    By bartybasher in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2003, 03:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21