Extracting numbers from a string and performing calculations

I am writing a program which simulates a point of sale software. My program has to search for a barcode in a file and than output the chosen products and calculate the prices in order to give receipt.
I managed to do everything but the last part (calculate prices). Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;

void KeyWord(ifstream &FileSearch)
{
    string line;
    string letters[5];
    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;
}


The file with products:

1
2
3
4
5
6
7
8
9
10
$0.50 Chips 01c
$1.5 Juice 02j
$1.00 Chocolate 03c
$0.20 Pen 04p
$30.00 Backpack 05b
$35.25 Bag 06b
$10.50 Ball 07b
$15.22 Toy 08t
$40.00 Wi-Fi Router 09wr
$4.00 Generic HDMI cable 010hc


I tried putting the stod(line.substr(position + 1 )); into a new variable and perform calculation
1
2
sum = stod(line.substr(position + 1 ));
sum = sum * 5;

but since this is in a while loop my prices are just getting multiplied

I need a way to perform addition in order to get a total price of ordered products.
Try : sum += stod(line,substr(position +1));
Yes, it works!

Thanks a lot!
Topic archived. No new replies allowed.