0

Straight to the point, I have a task -> program asks for a price input, then, in the given csv file, it compares the input price to the csv file price (last value of the line). Then the program should print out the lines in which the price is the same as input or LOWER. Note, that the csv file is as it is, some lines are "broken" if you can say so, not even. So far I removed the unnecessary spacings in lines and only perform the code if line is not empty. I had an idea to write the values of each line (values are separated by comma) to a vector and then compare to price with specific vector index, but didn't manage to get it running as it should. Please help.

fstream file("db.csv", ios::in);
float price;
string line;

cin >> price;

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

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

Here is the db.csv file data as it is (with all the whitespaces and empty lines).

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

HOW THE OUTPUT SHOULD LOOK LIKE

Muffin
  • 55
  • 1
  • 1
  • 8
  • 1
    I can't see in your code the part where you try to split each line into a vector? This sounds like a good approach, maybe look at this answer https://stackoverflow.com/a/46943631/5194459 and let us know how you get on. – Dominic Price Dec 03 '21 at 17:05
  • @drescherjm Yeah, that probably is a good idea. Edited the question, now the file data is as text there. – Muffin Dec 03 '21 at 18:19

1 Answers1

0

You can do this without std::vector as shown below:

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

int main()
{
    std::ifstream inputFile("input.txt");
    std::string word1,word2,word3,word4,word5;
    
    float price;//price take from user
    std::cin >> price;
    
    float priceFile; //price read from file
    if(inputFile)
    {
        while(std::getline(inputFile, word1,','),//read the first word
        std::getline(inputFile, word2,','),//read the second word
        std::getline(inputFile, word3,','),//read the third word
        std::getline(inputFile, word4,','),//read the fourth word
        std::getline(inputFile, word5,'\n'))//note the '\n' this time
        {
            std::istringstream ss(word5);
            ss >> priceFile;
            //check 
            if(priceFile <= price)
            {
                std::cout<<word1 <<" "<<word2<<" "<<word3<<" "<<word4<<" "<<word5<<std::endl;
            }
            
        }
    }
    else 
    {
        std::cout<<"Input file cannot be openede"<<std::endl;
    }
}

The output of the program can be seen here.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • This worked just fine, thank you! I would like to add that you should use the !line.empty() and the erase part, which my code uses, because it prints out with unnecessary spaces and empty lines. – Muffin Dec 04 '21 at 09:54
  • @Muffin You're welcome. Yes you can modify the above example according to your needs, like for example if you don't need empty lines to print then add `!line.empty()` etc etc. – Jason Dec 04 '21 at 09:55