简体   繁体   中英

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;
   }

You can try like this.

while (getline(readSearch, line))
{
    int pos = line.find_last_of('$');
    double num = stod(line.substr(pos + 1, line.size() - pos - 1));
    //cout << num << endl;
}

Thanks !!!

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM