How to separate multiple numbers into one array from a sentence

I want only the numbers after $ signs (read from a file into a string) from the following sentence to store them in an array.

Price 1=$6.00 Price 2=$5.00 Price 3=$10.00

Any idea?
Last edited on
Use a function like getline() or .ignore() to skip everything up to (and including) a '$', then read a price, and repeat.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>

int main()
{
  std::string s = "Price 1=$6.00 Price 2=$5.00 Price 3=$10.00";
  std::istringstream ss( s );
  double price;
  while (ss.ignore( std::numeric_limits <std::streamsize> ::max(), '$' ) >> price)
  {
    std::cout << std::fixed << std::setprecision(2) << price << "\n";
  }
}
6.00
5.00
10.00

Hope this helps.
Topic archived. No new replies allowed.