C++ 如何从输入文件中获取值的总和?

C++ 如何从输入文件中获取值的总和?,c++,C++,您好,有人能告诉我如何从输入文本文件中仅获取注释总数(1x20;10x50,100x50=总共111个注释)的值吗 我用line.find()尝试了一些东西,但是当我有两位/三位数字时,比如10 x 50 USD,100 x 50 USD,它给了我错误: Unhandled exception exception: std::out_of_range at memory location 0x005FDB1C 这是代码 int total=0; string str2="USD"; std::

您好,有人能告诉我如何从输入文本文件中仅获取注释总数(1x20;10x50,100x50=总共111个注释)的值吗

我用
line.find()
尝试了一些东西,但是当我有两位/三位数字时,比如10 x 50 USD,100 x 50 USD,它给了我错误:

Unhandled exception exception: std::out_of_range at memory location 0x005FDB1C
这是代码

int total=0;
string str2="USD";
std::ifstream file("input.txt");
            if (file.is_open()) {
                std::string line;
                while (getline(file, line)) {

                    if (line.find(str2) != string::npos) {
                        cout << line.substr((line.find("USD")-7))<< '\n';
                        int d = stoi(line.substr((line.find(" x")- 1)));
                        total = total + d;
                        cout << "Total sum: " << total;
                            }
                }
                file.close();
            }

一种可能的方法是保存从变量中的
line.find(str2)
获得的位置。然后从行的开头到该位置创建一个子字符串:

auto usd_pos = line.find(str2);
if (usd_pos != std::string::npos)
{
    auto value_string = line.substr(0, usd_pos);
    ...
}
一旦获得了包含数字的子字符串,将其放入输入字符串流(
std::istringstream
)中,并从该流中读取整数和
'x'

int first_value, second_value;
char dummy_x_char;

std::istringstream stream_with_values(value_string);
stream_with_values >> first_value >> dummy_x_char >> second_value;

使用绝对位置解析字符串容易出错。 当您有需要存储到类型化变量中的标记分隔值(特别是如果标记是空白)时,使用流(例如
std::stringstream
)就更容易了

这是一种方法:

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

int main() {
    int total=0;
    std::string str2="USD";
    std::ifstream file("input.txt");
    if (file.is_open()) {
        std::string line;
        while (std::getline(file, line)) {
            if (line.find(str2) != std::string::npos) {
                std::stringstream ss(line);
                int number_of_notes, value;
                std::string operatorX, USD_string;
                ss >> number_of_notes>> operatorX >> value >> USD_string;
                total += number_of_notes;
                std::cout << "Total sum: "<< total << std::endl;
            }
        }
        file.close();
    }
}
#包括
#包括
#包括
#包括
int main(){
int-total=0;
std::string str2=“USD”;
std::ifstream文件(“input.txt”);
if(file.is_open()){
std::字符串行;
while(std::getline(文件,行)){
if(line.find(str2)!=std::string::npos){
std::stringstream ss(线路);
注释的整数,值;
std::字符串运算符X,USD_字符串;
ss>>票据数量>>运算符X>>价值>>美元字符串;
总数+=票据数量;

STD::CUT

这里是如何使用C++正则表达式实现解析:

#include <regex>
...

        while (std::getline(file, line)) {
            std::smatch match;
            if (std::regex_match(line, match, std::regex("\\s*([0-9]+) x ([0-9]+) USD\\s*"))) {
                int number_of_notes = std::stoi(match[1]);
                int value = std::stoi(match[2]);
                total += number_of_notes * value;
                std::cout << "Total sum: "<< total << std::endl;
            }
        }
#包括
...
while(std::getline(文件,行)){
std::smatch匹配;
如果(std::regex_匹配(行,匹配,std::regex(\\s*([0-9]+)x([0-9]+)USD\\s*)){
注释的整数=标准::stoi(匹配[1]);
int value=std::stoi(匹配[2]);
合计+=注释数量*值;

std::请澄清“它给了我错误”-到底发生了什么?哪个错误?顺便说一句,您可以简化代码和/或输入文件-如果可以缩小范围,只保留有问题的部分。另请参见。简化将使代码更易于理解,因此您将获得更好的答案,甚至可以自己修复错误。这是我的错误
未处理的异常异常:std::out_内存位置0x005FDB1C处的\u范围。
#include <regex>
...

        while (std::getline(file, line)) {
            std::smatch match;
            if (std::regex_match(line, match, std::regex("\\s*([0-9]+) x ([0-9]+) USD\\s*"))) {
                int number_of_notes = std::stoi(match[1]);
                int value = std::stoi(match[2]);
                total += number_of_notes * value;
                std::cout << "Total sum: "<< total << std::endl;
            }
        }