Re: String of data into an array
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
*/
--
Bob R
POVrookie