///////////////////////////////////////////////////////////////////// // Marshalling.cpp - demonstrates how to convert a collection // // of doubles into a string and back again // // // // Jim Fawcett, CSE691/791 - SW Modeling & Analysis, Fall 2001 // ///////////////////////////////////////////////////////////////////// #include #include #include void main() { std::cout << "\n Demonstrating Conversions Needed for Project #2 " << "\n =================================================\n"; double stuff[4] = { 3.1415927, -3.33333e-5, 0, 1.2345e18 }; // convert a collection of doubles to a string std::ostringstream convert; int i; for(i=0; i<3; i++) convert << stuff[i] << ", "; convert << stuff[3]; std::string temp = convert.str(); std::cout << "\n string representation of doubles:\n\n " << temp << std::endl; // extract a collection of doubles from a string std::cout << "\n Extracting doubles from string\n"; double d; std::istringstream parse(temp); do { parse >> d; // the next few lines remove the comma and space // placed between doubles in the code above. // This only works for exactly that configuration, // e.g. comma seperated doubles if(!parse.good()) { parse.clear(); // force the stream state to good char junk; parse >> junk; // get rid of comma } else std::cout << "\n found double = " << d; } while(!parse.eof()); std::cout << "\n found double = " << d; std::cout << "\n\n"; }