//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Simple example of C++ stringstreams
#include<iostream>
#include<sstream>
#include<string>
using namespace std;
int main()
{
string input;
cout<< "Please enter some numbers speretaed by space and press enter once done: ";
getline(cin, input);
cout<< "input = " << input << endl;
stringstream memString(input, ios_base::in); //stringstream( string s, openmode mode )
cout<<"memString = " << memString << endl;
cout<<"memString (contents) = " << memString.str() << endl;
double summ = 0;
double temp;
while (memString >> temp)
{
summ+= temp;
}
cout<<"summ = " << summ << endl;
return 0;
}
The output is as follows:
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEigLPotgB-E3kTrLkQ0sdIu0SVuQlCuFR8GllC3Il_6gDYz8bnRHIQ12UvHluRHK3Vu2TLG-m2jQ6NwT7VwKpUF_ER1ZGC_Nl77YVULl7ueszyI0zYwxmaafVP27or9VOtyNkQ9TbcG6AhV/s400/zg_stringstream1.jpg)
See Also:
C++ Reference on stringstreams
Using Stringstreams in C++
String Stream example
String Streams Constructors
No comments:
Post a Comment