By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,199 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Retrieving numbers from a string

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)
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


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)

Jul 22 '05 #2

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)


Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.