Search code examples
c++fstream

Extract and use specific data from a text file in C++


Ok, I managed to extract the data I wanted from a file, but there is a problem when trying to calculate those extracted numbers (perform addition).

The numbers are stored here cout << stod( line.substr( position + 1 ) ) << endl;

I tried like this

   sum = stod( line.substr( position + 1 ) );
   sum = sum * 5.0;
   cout << sum << endl;

Since this is in a while loop, my program performes the calculation again adn again, so I get each price multiplied by five, but I only want to do addition like: sum+sum+sum+sum+sum. I even wrote that and I of course get the same thing.

   #include <iostream>
   #include <string>
   #include <fstream>
   #include <cstdlib>
   using namespace std;

   void KeyWord(ifstream &FileSearch)
   {
string line;
string letters[5];
long sum[6];
ifstream readSearch;

cout<< "Enter a barcode of a product: \n";
cin >> letters[0];
cin >> letters[1];
cin >> letters[2];
cin >> letters[3];
cin >> letters[4];
readSearch.open("Products.txt");
if(readSearch.is_open())
{
    while (getline(readSearch, line))
    {
        while (line.find(letters[0])!=string::npos || line.find(letters[1])!=string::npos || line.find(letters[2])!=string::npos || line.find(letters[3])!=string::npos || line.find(letters[4])!=string::npos)
        {
            cout << line << "\n";
            auto position = line.find( "$" );
            if( position <= line.size() )
            {
                cout << stod( line.substr( position + 1 ) ) << endl;
                break;
            }
        }
    }
}
   }

   int main()
   {
ifstream file("Products.txt");
KeyWord(file);
return 0;
   }

Solution

  • Here's some brute force for you to play with.

    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    
    double getPrice( string inputLine )
    {
        auto position = inputLine.find( "$" );
    
        if( position <= inputLine.size() )
        {
            return stod( inputLine.substr( position + 1 ) );
        }
    
        return 0;
    }
    
    string getBarCode( string inputLine )
    {
        auto position = inputLine.find( '$' );
    
        position -= 1;
    
        string barCode;
    
        while( isspace( inputLine[ position ] ) )
        {
            --position;
        }
    
        while( ! isspace( inputLine[ position ] ) )
        {
            barCode.insert( barCode.begin(), inputLine[ position-- ] );
        }
    
        return barCode;
    }
    
    int main( int argc, char** argv )
    {
        cout << "Enter barcode: " << endl;
    
        string barcodeInput;
        getline( cin, barcodeInput );
    
        ifstream inputFile( "Products.txt" );
    
        if( inputFile.is_open() )
        {
            string inputLine;
    
            while( getline( inputFile, inputLine) )
            {
                auto price = getPrice( inputLine );
    
                if( price != 0 )
                {
                    if( getBarCode( inputLine ) == barcodeInput )
                    {
                        cout << "Price: " << price << endl;
    
                        inputFile.close();
    
                        return 0;
                    }
                }
            }
    
            cout << "No matching barcode." << endl;
    
            inputFile.close();
        }
    
        return 0;
    }