Retrieving numbers from a string

J

Jesse

I'm trying to read numbers from a string with the format
'(1,2,3,4,5)'. Each number needs to go into the array, number[].
If the input string is not formatted as above, an error message will
be outputted. The numbers do not have to be of type int(can be
fractions) I know I need to separate the string into smaller
substrings then convert the substrings to type double to be placed
into the array but I'm stuck at what to do first.

Can anyone help me with this?


Thanks,
Jesse([email protected])
 
M

Moonlit

Hi,


Jesse said:
I'm trying to read numbers from a string with the format
'(1,2,3,4,5)'. Each number needs to go into the array, number[].
If the input string is not formatted as above, an error message will
be outputted. The numbers do not have to be of type int(can be
fractions) I know I need to separate the string into smaller
substrings then convert the substrings to type double to be placed
into the array but I'm stuck at what to do first.

Can anyone help me with this?

Something like the following should work.

Replace '(' and ',' with space then use stringstream to retrieve doubles:

#include <algorithm>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

replace_if( Number.begin(), Number.end(), bind2nd( equal_to<char>(), '(' ),
' ' );
replace_if( Number.begin(), Number.end(), bind2nd( equal_to<char>(), ')' ),
' ' );
replace_if( Number.begin(), Number.end(), bind2nd( equal_to<char>(), ',' ),
' ' );

stringstream NumberStream( Number );
double Frac;
vector< double > NumberVec;

wihle( NumberStream >> Frac )
{
NumberVec.push_back( Frac );
}


Regards, Ron AF Greve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top