Thread: Ok, Sort Problem...

  1. #16
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    nevermind fixed it...had to tell it which header alittle differently.

    also if one you gets bored, you think you could comment his code?...
    I've got some of it figured out, but arrays are greek to me....
    I seem to learn better from commented code and messin with it, so if someone
    doesn't mind. Thanks.

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It might be easier to just add "using namespace std;" to your code instead of all these other using statements. It'll reference stuff that you won't need, but considering that this program is just being written so you can practice and learn, it's not a huge deal at all, and, that way you won't get errors everytime you add something new to your code.

  3. #18
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    thats what I ended up doing

  4. #19
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I'll comment his code if you promise to go and read a tutorial on C++ arrays and loops.

  5. #20
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    ok ok lol...I'll read on them, suggestion on source? I have a c++ by example book....helps better in some areas than others.....

  6. #21
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    /*
      The next three lines simply include all
      the necessary packages
    */
    #include <iostream>
    #include <string>
    using namespace std;
    
    /*
      This declares a constant. Although the
      two words are completely opposite, think
      of a constant as a variable that never
      changes
    */
    const int numnames = 5;
    
    int main()
    {
    /*
      names is an array of strings, length is
      numnames, the constant value of 5
    */
      string names[numnames];
    /*
      for (starting values;
           condition (if true, loop executes);
           what to increment variables by)
      {
          code to execute;
      }
    
      If you have multiple starting values,
      etc., you can separate them with commas.
    
      If you have just one line in the loop,
      you can omit the {}'s. Make sure you
      don't have a ; after the for statement.
    */
    /*
      Reads in data to sort
    */
      for (int c=0; c < numnames; c++)
      {
        cout<<"Enter Name # "<<c<<": ";
        cin>>names[c];
      }
    /*
      Main logic loop
      - Compares each string
      - Depending on results, will swap
      strings to eventually smooth
      everything into correct order
    */
      for (int c1=0; c1 < numnames-1; c1++)
      {
        int min = c1;
    /*
      A nested loop - a loop inside a loop
    */
        for (int c2=c1+1; c2 < numnames; c2++)
          if ( names[min] > names[c2] )
            min = c2;
        if ( c1 != min )
          names[c1].swap(names[min]);
      }
    /*
      This loop reads out the sorted strings
    */
      for ( c=0; c<numnames; c++)
        cout<<names[c]<<endl;
    }
    Just any half-decent tutorial from Google will do. If you have a book, use that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. heap sort problem
    By Cpro in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2008, 04:54 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Using Vectors (cont) - Sort Problem
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2007, 06:31 AM
  5. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM