Home > database >  Line spacing removal, empty line ignoring
Line spacing removal, empty line ignoring

Time:12-05

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?

fstream file("db.csv", ios::in);
string value1, value2, value3, value4, value5, line;
float inputPrice, priceFile;

cin >> inputPrice;

if (file.is_open()) {
    cout << "result:" << endl;

    while (getline(file, line)) {
        if (!line.empty()) {
            line.erase(remove(line.begin(), line.end(), ' '), line.end());

            getline(file, value1, ',');
            getline(file, value2, ',');
            getline(file, value3, ',');
            getline(file, value4, ',');
            getline(file, value5, '\n');

            istringstream str(value5);
            str >> priceFile;

            if (priceFile <= inputPrice) {
                cout << value1 << " " << value2 << " " << value3 << " " << value4 << " " << 
                value5 << endl;
            }
        }
    }

DB.CSV FILE DATA BELOW

Riga,Kraslava,Pr,15:00,11.00

Riga ,Kraslava,Pr ,18:00,11.00
  Kraslava,Riga,Pr,08:00,11.00
Kraslava,Daugavpils,Ot ,10:00, 3.00
Ventsplis,8.00,Liepaja,Sv,20:00
Dagda,Sv

Rezekne,Riga,Tr,13:00,10.50
Dagda,Kraslava,  Ce,18:00,  2.50
Dagda,Kraslava,Ce,18:00,2.50,Sv
  Riga,Ventspils,  Pt,09:00  ,  6.70

Liepaja,Ventspils,Pt,17:00,5.50

OUTPUT BELOW

5.90
result:
Kraslava Daugavpils Ot  10:00  3.00
Dagda Kraslava Ce 18:00 2.50,Sv

Liepaja Ventspils Pt 17:00 5.50

REQUIRED OUTPUT BELOW

5.90
result:
Kraslava Daugavpils Ot 10:00 3.00
Dagda Kraslava Ce 18:00 2.50
Liepaja Ventspils Pt 17:00 5.50

CodePudding user response:

If you don't want empty lines to be printed then you can use the below shown program:

#include <iostream>
#include <sstream>
#include <fstream>

int main()
{
    std::ifstream inputFile("input.txt");
    std::string word1,word2,word3,word4,word5,line;
    
    float price;//price take from user
    std::cin >> price;
    
    float priceFile; //price read from file
    if(inputFile)
    {
        while(std::getline(inputFile,line) )//read line by line
        {
           //check if line is/isnot empty
           if(!line.empty())
           {
              std::istringstream ss(line);
            while(std::getline(ss, word1,','),//read the first word
                std::getline(ss, word2,','),//read the second word
                std::getline(ss, word3,','),//read the third word
                std::getline(ss, word4,','),//read the fourth word
                std::getline(ss, word5) )
                {
                    
                    std::istringstream ss2(word5);
                    ss2 >> priceFile;
                    
                    //check 
                    if(priceFile <= price)
                    {
                        //remove space from here if you need
                        std::cout<<word1 <<" "<<word2<<" "<<word3<<" "<<word4<<" "<<priceFile<<std::endl;
                    }
                    
                } 
           }
            
            
        }
        
    }
    else 
    {
        std::cout<<"Input file cannot be openede"<<std::endl;
    }
}

The output of the program can be seen here.

  •  Tags:  
  • c
  • Related