Blank lines "\n"

This is a discussion on Blank lines "\n" within the C++ Programming forums, part of the General Programming Boards category; I am trying to sort these lines from a .txtfile: 12/01/2000,1649 12/01/1998,1746 12/01/1997,1846 12/01/1996,1946 The output is right but with ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    Blank lines "\n"

    I am trying to sort these lines from a .txtfile:

    12/01/2000,1649
    12/01/1998,1746
    12/01/1997,1846
    12/01/1996,1946

    The output is right but with lines inbetween. It should not be blank lines inbetween. I cant figure it out what it depends on.

    Output:

    12/01/1996,1946

    12/01/1997,1846

    12/01/1998,1746
    12/01/2000,1649

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your program gives me a segmentation fault, so I can't run it to be sure, but I would be willing to bet at least a dime that every date (except for the first) begins with a \n character. (Normally getline stops at a \n, but you told it not to stop until it saw a comma, and >> with an int is going to leave a \n in the input stream.)

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    This was strange. I am not sure of what is happening. Somehow a \n is happening.
    I tried this out but know it not correct thogh but peheps more near of what should be done:
    I put a char after and then the output look like this. There was no \n anyway but it seems that Comma took away: 1 in the beginning and the First row should be the last row.

    12/01/2000,1649
    2/01/1998,1746
    2/01/1998,1946
    2/01/2000,1535

    Code:
    char Comma;
    
    	while	(    getline(Sort, Symbol, ',' )   )           		
    	{
    		t = (t + 1);		// Count
    		
    			Sort >> Action;	
    			Sort >> Comma;
    			
    			
    
    			Values[t].Symbol2 = Symbol;
    			Values[t].Action = Action;
    		
    		
    	}

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    The output is right but with lines inbetween. It should not be blank lines inbetween. I cant figure it out what it depends on.
    The problem is that your formatted input with the overloaded operator>> for istream leaves the newline character in the input stream. You need to ignore this extra character.

    Another problem is that you are not taking advantage of the dynamic nature of std::vector. At the moment your program works fine if the textfile has exactly 5 lines. You should use push_back() instead:
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <cmath>
    #include <algorithm>
    #include <limits>
    #include <ios>
    
    struct Value
    {
        int Action;
        std::string Symbol;
    };
    
    using namespace std;
    
    int main()
    {
        std::vector<Value> Values;
    
        ifstream SortInput("Sort.txt");
        std::string Symbol;
        while (getline(SortInput, Symbol, ','))
        {
            int Action;
            SortInput >> Action;
            SortInput.ignore(numeric_limits<streamsize>::max(), '\n');
    
            Value value = {Action, Symbol};
            Values.push_back(value);
        }
        SortInput.close();
    
        std::sort(Values.begin(), Values.end());
    
        ofstream SortOutput("Sort1.txt");
        for (std::vector<Value>::iterator i = Values.begin(), end = Values.end();
             i != end; ++i)
        {
            SortOutput << i->Symbol << ',' << i->Action << '\n';
        }
    
        return 0;
    }
    
    bool operator < (const Value& x, const Value& y)
    {
        return ((x.Symbol < y.Symbol)
            || (x.Symbol == y.Symbol && x.Action <= y.Action));
    }
    I included <limits> and <ios> for the istream ignore() functionality.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Wow... thank you very much. I have to look at this code carefully to learn it. You are right about the pushback. I haven&#180;t really learned how to use that.
    I just reminded myself that this also could be used to not read in the last charecter. It did work when I added it like this:
    Though I not sure if it is better or as good as your approach. However I will learn the pushback method.
    Thank you !

    Code:
    Sort >> Action;	
    Sort.get();

  6. #6
    Registered User bradszy's Avatar
    Join Date
    Jan 2008
    Posts
    114
    Quote Originally Posted by Coding View Post
    Wow... thank you very much.
    Yeah, laserlight, Elysia, and a few others generally get that alot. And so they should.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I have to agree with that again, Elysia too and a few others ofcourse ! :-)

  8. #8
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    It was strange. I did a new project and changed my code to the above solution. If I have 4 lines in the file as before it works fine. But if I add 1 line more to the file and run the application I get an Error message "Debug Assertion Failed!"

    This did happend when I didn&#180;t use push_back. Any ideas why this is happening.
    Last edited by Coding; 02-18-2008 at 10:48 AM.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Where does the assert happen?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    It says:
    VC\include\algorithm
    Line: 2980

    Expression: invalid operator <
    I dont know if that says something about the problem.
    The code works using 1-4 lines in the Sort.txt but with 5 lines this error comes up.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,449
    Post the file with 5 lines and the last code
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    I did found out the problem I think: These lines worked:

    12/01/2000,1649
    12/01/1998,1746
    12/01/1997,1846
    12/01/1996,1946

    But not with these 5 lines:

    12/01/2000,1649
    12/01/1998,1746
    12/01/1997,1846
    12/01/1996,1946
    12/01/1996,1946

    If you see, the last line is identical as the line before. If you change for example 1946 to something else but 1946. For example 1947. Then it will work.

    I changed the bool operator to this:

    Code:
    bool operator < (const Value& x, const Value& y)
    {
        return ((x.Symbol < y.Symbol)
        || (x.Symbol == y.Symbol && x.Action < y.Action));
    }
    Before it was x.Action <= y.Action so it should have been this that was the problem.
    Last edited by Coding; 02-18-2008 at 06:02 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Coding View Post
    I have found out one thing what it could depend on. the code works with these 4 lines:

    12/01/2000,1649
    12/01/1998,1746
    12/01/1997,1846
    12/01/1996,1946

    But not with these 5 lines:

    12/01/2000,1649
    12/01/1998,1746
    12/01/1997,1846
    12/01/1996,1946
    12/01/1996,1946

    If you see, the last line is identical as the line before. If you change for example 1946 to something else but 1946. For example 1947. Then it will work.
    Also if you change the last line to this: 11/01/1996,1946 this will work. So it doesn´t matter wich one of the strings that are different.

    Why does this depend on ? It shouldn´t be like that as it is possible to have simular lines.
    Because for the last two items on your list (call them x and y), both x < y and y < x. This does bad things to sort. Change your operator < so that that doesn't happen (maybe by changing that last <= to a <).

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes that was the problem. There is one more thing that will not work with the sort for these 2 lines as a example:

    12/01/2000,1600
    12/02/1998,1600

    As this is a date and a time the sort should look like below as year 1998 comes before 2000 but it wont as I have understand it begins with 12/01 and thinks that this is the lowest value and then contine with 12/02. (So the sort of the 2 above lines will be the same as the 2 above lines)

    12/02/1998,1600
    12/01/2000,1600

    What could be needed to do here. Is it necessary to split the date in anyway to only numbers etc... or is there any other way ?
    Last edited by Coding; 02-18-2008 at 07:52 PM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    To get date order, I think you're going to have to parse those dates yourself, yes. Fortunately, we're already dealing with structs, so you can replace std::string Symbol2 with int month, int day, int year, and re-examine your input routine.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with deleting completely blank lines
    By dnguyen1022 in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 10:51 AM
  2. Discarding blank lines?
    By Blurr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 11:30 AM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. Question About Blank Lines in Text Files
    By Zildjian in forum C++ Programming
    Replies: 11
    Last Post: 10-16-2004, 04:31 PM
  5. Blank Lines in a menu
    By ColdFire in forum Windows Programming
    Replies: 2
    Last Post: 07-05-2002, 03:51 PM

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