String of data into an array

S

Sudzzz

Hi,
I'm relatively new to programming and would like to convert a string
of data that is extracted from a program into an array.
My string of data looks like this.

{105,0,196,3,54,154,12,53,125,46}

I would like to convert this char string into an array. Please help me
out.

Thanks,
Sudeep
http://sudeeppillai.blogspot.com
 
B

BobR

Sudzzz wrote in message...
Hi,
I'm relatively new to programming and would like to convert a string
of data that is extracted from a program into an array.
My string of data looks like this.

{105,0,196,3,54,154,12,53,125,46}

I would like to convert this char string into an array. Please help me
out.

int array[] = {105,0,196,3,54,154,12,53,125,46}; // <G>

Seriously, look into std::stringstream.
A rough example:

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main(){
// std::istringstream in( "105,0,196,3,54,154,12,53,125,46" );
std::string stin( "105,0,196,3,54,154,12,53,125,46" );
std::istringstream in( stin );
std::vector<int> array;

for( std::string line; std::getline( in, line, ',' ); ){
std::stringstream conv( line );
int num(0);
conv >> num;
array.push_back( num );
conv.clear(); conv.str("");
} // for(getline)

for( std::size_t i(0); i < array.size(); ++i ){
std::cout<< array.at( i ) <<std::endl;
} // for(i)
} // main()
/* - output -
105
0
196
3
54
154
12
53
125
46
*/
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top