Thread: Line spacing removal, empty line ignoring

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    3

    Line spacing removal, empty line ignoring

    I've got an issue with unnecessary spacing removal and empty line ignorance. So the code below -> reads a line from a file, gets all 5 values from the line (value1, ..., value5). Then the value5 is checked if it's a float, then it gets compared with a user input price and if the line's float value is less or equal to the user's input price, then that line's data is being printed out. That part is working, but it prints the data out with an empty line, unnecessary spacings just as in the given data file (db.csv -> see below). I guess the getline part in which it assigns the values to value1 etc is working ignoring the earlier spacing deletion. Any ideas?

    Code:
    [COLOR=var(--highlight-color)]fstream [COLOR=var(--highlight-literal)]file[/COLOR]([COLOR=var(--highlight-variable)]"db.csv"[/COLOR], ios::in)[/COLOR][COLOR=var(--highlight-color)];[/COLOR]
    string value1, value2, value3, value4, value5, line;
    [COLOR=var(--highlight-namespace)]float[/COLOR] inputPrice, priceFile;
    
    cin >> inputPrice;
    
    [COLOR=var(--highlight-keyword)]if[/COLOR] (file.[COLOR=var(--highlight-literal)]is_open[/COLOR]()) {
        cout << [COLOR=var(--highlight-variable)]"result:"[/COLOR] << endl;
    
        [COLOR=var(--highlight-keyword)]while[/COLOR] ([COLOR=var(--highlight-literal)]getline[/COLOR](file, line)) {
            [COLOR=var(--highlight-keyword)]if[/COLOR] (!line.[COLOR=var(--highlight-literal)]empty[/COLOR]()) {
                line.[COLOR=var(--highlight-literal)]erase[/COLOR]([COLOR=var(--highlight-literal)]remove[/COLOR](line.[COLOR=var(--highlight-literal)]begin[/COLOR](), line.[COLOR=var(--highlight-literal)]end[/COLOR](), [COLOR=var(--highlight-variable)]' '[/COLOR]), line.[COLOR=var(--highlight-literal)]end[/COLOR]());
    
                [COLOR=var(--highlight-literal)]getline[/COLOR](file, value1, [COLOR=var(--highlight-variable)]','[/COLOR]);
                [COLOR=var(--highlight-literal)]getline[/COLOR](file, value2, [COLOR=var(--highlight-variable)]','[/COLOR]);
                [COLOR=var(--highlight-literal)]getline[/COLOR](file, value3, [COLOR=var(--highlight-variable)]','[/COLOR]);
                [COLOR=var(--highlight-literal)]getline[/COLOR](file, value4, [COLOR=var(--highlight-variable)]','[/COLOR]);
                [COLOR=var(--highlight-literal)]getline[/COLOR](file, value5, [COLOR=var(--highlight-variable)]'\n'[/COLOR]);
    
                istringstream [COLOR=var(--highlight-literal)]str[/COLOR](value5);
                str >> priceFile;
    
                [COLOR=var(--highlight-keyword)]if[/COLOR] (priceFile <= inputPrice) {
                    cout << value1 << [COLOR=var(--highlight-variable)]" "[/COLOR] << value2 << [COLOR=var(--highlight-variable)]" "[/COLOR] << value3 << [COLOR=var(--highlight-variable)]" "[/COLOR] << value4 << [COLOR=var(--highlight-variable)]" "[/COLOR] << 
                    value5 << endl;
                }
            } [COLOR=var(--highlight-color)]    }
    [/COLOR]
    DB.CSV FILE DATA BELOW

    Code:
    [COLOR=var(--highlight-color)]Riga,Kraslava,Pr,[/COLOR][COLOR=var(--highlight-namespace)]15[/COLOR][COLOR=var(--highlight-color)]:[/COLOR][COLOR=var(--highlight-namespace)]00[/COLOR][COLOR=var(--highlight-color)],[/COLOR][COLOR=var(--highlight-namespace)]11.00[/COLOR]
    
    Riga ,Kraslava,Pr ,[COLOR=var(--highlight-namespace)]18[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR],[COLOR=var(--highlight-namespace)]11.00[/COLOR]
      Kraslava,Riga,Pr,[COLOR=var(--highlight-namespace)]08[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR],[COLOR=var(--highlight-namespace)]11.00[/COLOR]
    Kraslava,Daugavpils,Ot ,[COLOR=var(--highlight-namespace)]10[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR], [COLOR=var(--highlight-namespace)]3.00[/COLOR]
    Ventsplis,[COLOR=var(--highlight-namespace)]8.00[/COLOR],Liepaja,Sv,[COLOR=var(--highlight-namespace)]20[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR]
    Dagda,Sv
    
    Rezekne,Riga,Tr,[COLOR=var(--highlight-namespace)]13[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR],[COLOR=var(--highlight-namespace)]10.50[/COLOR]
    Dagda,Kraslava,  Ce,[COLOR=var(--highlight-namespace)]18[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR],  [COLOR=var(--highlight-namespace)]2.50[/COLOR]
    Dagda,Kraslava,Ce,[COLOR=var(--highlight-namespace)]18[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR],[COLOR=var(--highlight-namespace)]2.50[/COLOR],Sv
      Riga,Ventspils,  Pt,[COLOR=var(--highlight-namespace)]09[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR]  ,  [COLOR=var(--highlight-namespace)]6.70[/COLOR]
     [COLOR=var(--highlight-color)]Liepaja,Ventspils,Pt,[/COLOR][COLOR=var(--highlight-namespace)]17[/COLOR][COLOR=var(--highlight-color)]:[/COLOR][COLOR=var(--highlight-namespace)]00[/COLOR][COLOR=var(--highlight-color)],[/COLOR][COLOR=var(--highlight-namespace)]5.50
    [/COLOR]
    OUTPUT BELOW

    Code:
    [COLOR=var(--highlight-namespace)]5.90[/COLOR]
    result:
    Kraslava Daugavpils Ot  [COLOR=var(--highlight-namespace)]10[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR]  [COLOR=var(--highlight-namespace)]3.00[/COLOR]
    Dagda Kraslava Ce [COLOR=var(--highlight-namespace)]18[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR] [COLOR=var(--highlight-namespace)]2.50[/COLOR],Sv
     [COLOR=var(--highlight-color)]Liepaja Ventspils Pt [/COLOR][COLOR=var(--highlight-namespace)]17[/COLOR][COLOR=var(--highlight-color)]:[/COLOR][COLOR=var(--highlight-namespace)]00[/COLOR][COLOR=var(--highlight-color)][/COLOR][COLOR=var(--highlight-namespace)]5.50
    [/COLOR]
    REQUIRED OUTPUT BELOW

    Code:
    [COLOR=var(--highlight-namespace)]5.90[/COLOR]
    result:
    Kraslava Daugavpils Ot [COLOR=var(--highlight-namespace)]10[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR] [COLOR=var(--highlight-namespace)]3.00[/COLOR]
    Dagda Kraslava Ce [COLOR=var(--highlight-namespace)]18[/COLOR]:[COLOR=var(--highlight-namespace)]00[/COLOR] [COLOR=var(--highlight-namespace)]2.50[/COLOR] [COLOR=var(--highlight-color)]Liepaja Ventspils Pt [/COLOR][COLOR=var(--highlight-namespace)]17[/COLOR][COLOR=var(--highlight-color)]:[/COLOR][COLOR=var(--highlight-namespace)]00[/COLOR][COLOR=var(--highlight-color)][/COLOR][COLOR=var(--highlight-namespace)]5.50
    [/COLOR]
    Last edited by Salem; 12-04-2021 at 08:38 AM. Reason: Removed spam, but crayola remains

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,892
    Are you ever going to learn how to post code without all that markup?

    Are you going to stop pushing your spam urls?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,892
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-26-2012, 03:25 PM
  2. Ignoring line in C++ when reading from file
    By falzone in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2007, 12:08 AM
  3. Ignoring New Line Characters
    By Spencer Wallace in forum C Programming
    Replies: 2
    Last Post: 03-27-2006, 01:23 AM
  4. cin.get on an empty line and cin.clear
    By ^xor in forum C++ Programming
    Replies: 14
    Last Post: 07-11-2005, 05:28 AM
  5. Ignoring the Tab at the begining of the line..
    By NANO in forum C++ Programming
    Replies: 8
    Last Post: 05-03-2003, 10:56 PM

Tags for this Thread