P: n/a

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(ga******@aol.com)  
Share this Question
P: n/a

Hi,
"Jesse" <ga******@ufl.edu> wrote in message
news:9d**************************@posting.google.c om... 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. Thanks, Jesse(ga******@aol.com)  
P: n/a

Try a "while" loop around sscanf.
Jesse wrote: 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(ga******@aol.com)   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1240
 replies: 2
 date asked: Jul 22 '05
