Thread: Ok, Sort Problem...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    38

    Ok, Sort Problem...

    Ok in my mind this worked lol. Will this not work, I know its kinda long, but I thought it would get the job done. Am I just not doing something right?

    Code:
    // This is the main project file for VC++ application project 
    // generated using an Application Wizard.
    
    //#include <iomanip>
    //#using <mscorlib.dll>
    //#include <tchar.h>
    //#include <stdlib.h>
    //#include <conio.h>
    //using namespace System;
    //#include <stdlib.h>
    //#include <cstring>
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    main()
    {
    	string strName; 
    	string strName2; 
    	string strName3; 
    	string strName4;
    	string strName5;
    	string strNameHold;
    	string strAllNames = strName + strName2 + strName3 + strName4 + strName5;
        
        cout << "Alphabetical Order System \n\n\n";
    
    	cout << "Enter Name # 1: ";
    	cin >> strName;
    	cout << "\n";
    
    	cout << "Enter Name # 2: ";
    	cin >> strName2;
    	cout << "\n";
    	
    	cout << "Enter Name # 3: ";
    	cin >> strName3;
    	cout << "\n";
    	
    	cout << "Enter Name # 4: ";
    	cin >> strName4;
    	cout << "\n";
    	
    	cout << "Enter Name # 5: ";
    	cin >> strName5;
    	cout << "\n";
    
    	//STRNAME IS GREATEST
    	if (strName > strName2)
    	{
    strNameHold = strName;
    strName = strName2;
    strName2 = strNameHold;
    	}
    
    	if (strName > strName3)
    	{
    strNameHold = strName;
    strName = strName3;
    strName3 = strNameHold;
    }
    
    if (strName > strName4)
    	{
    strNameHold = strName;
    strName = strName4;
    strName4 = strNameHold;
    
    if (strName > strName5)
    	{
    strNameHold = strName;
    strName = strName5;
    strName5 = strNameHold;
    
    //STRNAME2 IS GREATEST
    
    if (strName2 > strName)
    	{
    strNameHold = strName2;
    strName2 = strName;
    strName = strNameHold;
    
    	}
    
    if (strName2 > strName3)
    	{
    strNameHold = strName2;
    strName2 = strName3;
    strName3 = strNameHold;
    	}
    
    if (strName2 > strName4)
    	{
    strNameHold = strName2;
    strName2 = strName4;
    strName4 = strNameHold;
    	}
    if (strName2 > strName5)
    	{
    strNameHold = strName2;
    strName2 = strName5;
    strName5 = strNameHold;
    	}
    //STRNAME3 IS GREATEST
    
    if (strName3 > strName)
    	{
    strNameHold = strName3;
    strName3 = strName;
    strName = strNameHold;
    
    	}
    
    if (strName3 > strName2)
    	{
    strNameHold = strName3;
    strName3 = strName2;
    strName2 = strNameHold;
    	}
    
    if (strName3 > strName4)
    	{
    strNameHold = strName3;
    strName3 = strName4;
    strName4 = strNameHold;
    	}
    if (strName3 > strName5)
    	{
    strNameHold = strName3;
    strName3 = strName5;
    strName5 = strNameHold;
    	}
    //STRNAME4 IS GREATEST
    
    if (strName4 > strName)
    	{
    strNameHold = strName4;
    strName4 = strName;
    strName = strNameHold;
    
    	}
    
    if (strName4 > strName2)
    	{
    strNameHold = strName4;
    strName4 = strName2;
    strName2 = strNameHold;
    	}
    
    if (strName4 > strName3)
    	{
    strNameHold = strName4;
    strName4 = strName3;
    strName3 = strNameHold;
    	}
    if (strName4 > strName5)
    	{
    strNameHold = strName4;
    strName4 = strName5;
    strName5 = strNameHold;
    	}
    //STRNAME5 IS GREATEST
    
    if (strName5 > strName)
    	{
    strNameHold = strName5;
    strName5 = strName;
    strName = strNameHold;
    
    	}
    
    if (strName5 > strName2)
    	{
    strNameHold = strName5;
    strName5 = strName2;
    strName2 = strNameHold;
    	}
    
    if (strName5 > strName3)
    	{
    strNameHold = strName5;
    strName5 = strName3;
    strName3 = strNameHold;
    	}
    if (strName5 > strName4)
    	{
    strNameHold = strName5;
    strName5 = strName4;
    strName4 = strNameHold;
    	}	
    	cout << strAllNames;
    	//cout << "\n" << strName << "\n" << strName2 << "\n" << strName3 << "\n" << strName4 << "\n" << strName5;
    	}
    	return 0;
    }
    	
    
    	
    	
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The compiler has absolutely no idea what you mean by string1 > string2. This would probably be a good method to use if you were sorting a series of numbers from least to greatest, but you're using strings - and the behavior of the > operator is not defined for strings. If you want to use this method you'll need to learn how to overload operators. Are you misunderstanding the method I decribed in your last thread?

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    52
    What exactly is it that your trying to do?

    Sean is right im confused why you are trying to compare strings with <> operators.....

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    well the thing is, that it worked fine with 1 if statement and it sorted it fine, its just when i added the others it got weird on me. *sigh*.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    well even though they are strings, they still have a intger value right? So one would be greater or lesser than the other.....

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    What exactly is it that your trying to do?
    Oh my freakin' heck, son! He's trying to sort strings alphabetically! You actually posted several times in the thread that this originated from, you of all people on this board right now should know this! Please stop posting in threads you don't understand with, "what exactly is it that you're trying to do". Just because everyone had to ask that question in your threads does not mean you have license to do it in others, especially since you don't even understand how to include header files.

    well the thing is, that it worked fine with 1 if statement and it sorted it fine, its just when i added the others it got weird on me. *sigh*.
    Oh - maybe the > operator is defined for strings - I forgot that you were using the C++ string class - the method I described is probably best suited for C-style string (simple arrays of char variables). I will leave this problem to someone who has more experience with the string class.

    If the greater than / less than operators are indeed overloaded to handle strings in terms of alphabetical sorting, then I would guess the problem is just a mistake in your if statements. Your code certainly could be compacted and potentially made simpler by using an array of strings and a loop instead. If noone else can help you with this problem further, you might benefit from trying your algorithm on some numbers instead.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Technically strings do have an integer value. In C++, variables can be "casted" from one type to another, and one type that does this quite well is the char type, because it is simply a number that the compiler knows to interpret as a character (see also: ASCII numbers). The problem is that C++ strings are actually classes, which is where someone can define their own data type and make operators to work with that data type. A common example is people overloading the '+' operator to concatenate (add) strings.

    So with that in mind, the < / > operators could really be defined for any number of purposes. The example I used earlier was string length, but you are referring to the string's value in terms of alphabetical sorting. What the operators actually were defined for, I'm not sure. I will look it up, but for the time being I still think someone more experienced with C++ Strings should be able to help you more.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Hmm off the top of my head:
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::cin;
    using std::string;
    const int numnames = 5;
    int main()
    {
      string names[numnames];
      for (int c=0; c < numnames; c++)
      {
        cout<<"Enter Name # "<<c<<": ";
        cin>>names[c];
      }
      // Sort using selection sort which is what you did the long way of
      for (int c1=0; c1 < numnames-1; c1++)
      {
        int min = c1;
        for (int c2=c1+1; c2 < numnames; c2++)
          if ( names[min] > names[c2] )
            min = c2;
        if ( c1 != min )
          names[c1].swap(names[min]);
      }
      for ( int c=0; c<numnames; c++)
        cout<<names[c]<<endl;
    }
    Note: I have not compiled this code so there may be a small mistake in there

    Sean: there comparison operators are done using the compare() member function:
    http://www.cppreference.com/cppstrin...s.html#compare
    Last edited by Thantos; 11-20-2004 at 12:42 PM.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    This might help in what you're trying to do. It's a similar problem.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Code:
    #include <iostream>
    #include <string>
    
    using std::cout;
    using std::cin;
    using std::string;
    const int numnames = 5;
    int main()
    {
      string names[numnames];
      for (int c=0; c < numnames; c++)
      {
        cout<<"Enter Name # "<<c<<": ";
        cin>>names[c];
      }
      // Sort using selection sort which is what you did the long way of
      for (int c1=0; c1 < numnames-1; c1++)
      {
        int min = c1;
        for (int c2=c1+1; c2 < numnames; c2++)
          if ( names[min] > names[c2] )
            min = c2;
        if ( c1 != min )
          names[c1].swap(names[min]);
      }
      for ( c=0; c<numnames; c++)
        cout<<names[c]<<endl;
    }
    I added some int declarations that Thantos forgot, and this reduces the number of errors. I do however still get the following two - I've never had these errors and can't figure out what they could be.

    26 - Name lookup of 'c' changed for new ANSI 'for' scoping
    11 - using obsolete binding at 'c'

    At the very least, the code makes sense in one's mind and should help. This is why Thantos has an astronomically high reputation.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    I tried the char, and I had a error the conversion between that and string.....I'm still messing with it. I've went over my if statements and I don't think I messed them up...but there are a good many of them, so I may have missed something.
    You guys are really helpful though, and I appreciate your responses.

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Sean: there comparison operators are done using the compare() member function:
    http://www.cppreference.com/cppstri...ls.html#compare
    Ahh - cool site. Thanks, man!

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok I fixed my code. The error you were describing sean is because I had declared a variable called c in the first for loop and in the last for loop. Modern scoping rules limit it to the for loop but since I forgot the int in the last loop it had to change the to the old scoping rules such that its alive after you declare it till the end of the function.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    38
    I keep getting

    fatal error C1010: unexpected end of file while looking for precompiled header directive

    for the code Thantos provided....

    any idea why?

  15. #15
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Copy and paste the code you are compiling back here, it compiles just fine for me after I add using std::endl to the code.

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