从 C++ 中的文本文件中提取和使用特定数据
Extract and use specific data from a text file in C++
好的,我设法从文件中提取了我想要的数据,但是在尝试计算这些提取的数字(执行加法)时出现问题。
数字存储在这里
cout << stod( line.substr( 位置 + 1 ) ) << endl;
我这样试过
sum = stod( line.substr( position + 1 ) );
sum = sum * 5.0;
cout << sum << endl;
由于这是一个 while 循环,我的程序再次执行计算,所以我将每个价格乘以五,但我只想做加法,如:sum+sum+sum+sum+sum。
我什至写了那个,我当然得到了同样的东西。
#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;
}
你可以这样试试
while (getline(readSearch, line))
{
int pos = line.find_last_of('$');
double num = stod(line.substr(pos + 1, line.size() - pos - 1));
//cout << num << endl;
}
谢谢!!!
这里有一些蛮力给你玩。
#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;
}
好的,我设法从文件中提取了我想要的数据,但是在尝试计算这些提取的数字(执行加法)时出现问题。
数字存储在这里 cout << stod( line.substr( 位置 + 1 ) ) << endl;
我这样试过
sum = stod( line.substr( position + 1 ) );
sum = sum * 5.0;
cout << sum << endl;
由于这是一个 while 循环,我的程序再次执行计算,所以我将每个价格乘以五,但我只想做加法,如:sum+sum+sum+sum+sum。 我什至写了那个,我当然得到了同样的东西。
#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;
}
你可以这样试试
while (getline(readSearch, line))
{
int pos = line.find_last_of('$');
double num = stod(line.substr(pos + 1, line.size() - pos - 1));
//cout << num << endl;
}
谢谢!!!
这里有一些蛮力给你玩。
#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;
}