Thread: Problems with sorting

  1. #16
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can't initialize a variable like this:
    Code:
    		int Action2 = 0;
    You can only define it at that stage. If there were a class definition, you could make a class constructor to fix it. I'm not sure how to fix this without digging into the book.

    Todd

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes I just figured out as I got your answer so I deleted the code just before.
    So I have compile success when declaring it like: int Action2
    But I only have this If I do this with the line that do the sort for Values:

    //std::sort(Values.begin(), Values.end());

    So something must be wrong here. The thing is that I will sort for the Action value. Perheps this should be specified in any way.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    #include <cmath>
    #include <algorithm>
    
    struct Value 
    	{
    		int Action2;
    		std::string Symbol2;
    	};
    
    using namespace std;
    int main () 
    { 
    	
    	char Comma;
    	int t = 0;
    	std::string Symbol;
    	int Action = 0;
    	std::vector<Value> Values (3);
    
    
    	ofstream Sort1;
      
    	Sort1.open ("Sort1.txt");
    	ifstream Sort ("Sort.txt");
    
    	while	(    getline(Sort, Symbol, ',')   )           		
    	{
    		t = (t + 1);		// Count
    		
    		Sort >> Action;		// Buy
    
    			Values[t].Symbol2 = Symbol;
    			Values[t].Action2 = Action;
    	
    		
    	}
    
    		//std::sort(Values.begin(), Values.end());
    
    		for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++)			//Number of Lines
    
    		{
    			Sort1 << 
    				Values[Rowsen].Symbol2 << ',' <<
    				Values[Rowsen].Action2
    			<< "\n";
    		}
    
    		return 0;
    }

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The problem is that C++ doesn't know how to sort these new-fangled Value objects, since you haven't told it how. You're going to have to define what < means for Values. Someone will chime in if I'm wrong, but I believe you need to define this function:
    Code:
    bool operator < (const Value& lhs, const Value& rhs) {
    // returns true if lhs < rhs, false otherwise
    // code goes here
    }
    (Technically you can call it anything you want, but if you call it something else you have to specify that function in your sort call.)
    This way the sort algorithm knows whether this guy is less than that one.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes that right ofcourse... I will check these functions out and laborate with this and see what I get... Thanks for your help !

  5. #20
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I am trying to do a sort so the contents in a .txt file that look like this:
    ABC,3
    DEF,2

    will look like this: (Ascending order for the number) (The sorting is for the numbers(Called Action2 in my code)
    DEF,2
    ABC,3

    I using stucts to manage this but have a problem to understand what I should do with the these lines that is doing the rules for the sort and then sorting it.
    I beleive it should be written "Bool operator" and if the x value is less than the y value inside the parathes Bool evaluates TRUE. I think the logics stands for this.

    Then it is many things I dont understand, like if the values x and y is exampleletters or should it stand something else here in my case ?
    The next line with return is the same, what should be written instead of x.Values ?
    For the sort that coming next.
    Should it only be written Values.begin() or do I have to refer to Action2 ?
    Code:
    bool operator < (const Value& x, const Value& y) 
     {
       return x.Values < y.Values
     };
       std::sort(Values.begin(), Values.end());
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream> 
    #include <string>  
    #include <vector>
    #include <cmath>
    #include <algorithm>
    
    struct Value 
    	{
    		int Action2;
    		std::string Symbol2;
    	};
    
    using namespace std;
    int main () 
    { 
    	
    	char Comma;
    	int t = 0;
    	std::string Symbol;
    	int Action = 0;
    	std::vector<Value> Values (3);
    
    
    	ofstream Sort1;
      
    	Sort1.open ("Sort1.txt");
    	ifstream Sort ("Sort.txt");
    
    	while	(    getline(Sort, Symbol, ',')   )           		
    	{
    		t = (t + 1);		// Count
    		
    		Sort >> Action;		// Buy
    
    			Values[t].Symbol2 = Symbol;
    			Values[t].Action2 = Action;
    	
    		
    	}
    
    		bool operator < (const Value& x, const Value& y) 
    		  {
    			return x.Values < y.Values
    		  };
    			std::sort(Values.begin(), Values.end());
    
    		for (int Rowsen = 1; Rowsen < (t + 1); Rowsen++)			//Number of Lines
    
    		{
    			Sort1 << 
    				Values[Rowsen].Symbol2 << ',' <<
    				Values[Rowsen].Action2
    			<< "\n";
    		}
    
    		return 0;
    }
    Last edited by Coding; 01-22-2008 at 08:26 AM.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And why do you think x.Values means anything? x is a Value -- your function declaration says so! -- so x contains a Symbol2 and an Action2. Same with y.

    And of course you can call your parameters of your functions anything you like, provided they aren't keywords like if, or macros. I like to use lhs and rhs, but no one's going to complain about x and y.

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Okay, so if x and y contains the Action2 and Symbol2 it makes a little bit more sense to me.

    So far that I can manage right now, I think I have to specify what value in the x and y values that is interesting for sorting.. In this case Action2. So I write: x.Action2 < y.Action2
    I dont know if this is correct:

    Code:
    bool operator < (const Value& x, const Value& y) 
    
     {
       return x.Action2 < y.Action2;
     };
         std::sort(Values.begin(), Values.end());
    The compiler says that:
    'operator <' : local function definitions are illegal
    a '{' which has not yet been matched

  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Don't define functions within other functions.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #24
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Even if I change this line to this. Still there is something that isn&#180;t correct. The Compiler isn&#180;t happy.
    Code:
    {
    return x.Action2 < y.Action2
    };

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are implementing the function "operator<" inside another function (main) in the code you posted above. You need to move this function out of there and above main (or below, if you declare a prototype above).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Thank You matsp... This was the mainproblem I had. I didn&#180;t know that this function had to be ouside the main. So now it is working. Couldn&#180;t understand what was wrong... Thanks...

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In standard C or C++ (rather than extensions such as gcc), you can NEVER define a function inside another function. Some other languages, such as Pascal, allows you to define a function inside another function - but C and C++ doesn't.

    You can define a class member function inside a class, that is just about the only time you can declare a function inside of curly brackets.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #28
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes okay I see. That was what I wondered also. I knew that Pascal could do it so I was a bit confused about it. Now I have a lot to experiment on :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problems.
    By Sedvan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2008, 01:13 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM