1: //input_string_stream.cpp
3: #include <iostream>
4: #include <string>
5: #include <sstream>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program illustrates an input string stream.";
11: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
13: cout << "\nFirst we create an input string stream object in memory, "
14: "initialized with a\nstring object containing some numerical "
15: "values (in string form). Then we read\nthe numerical values "
16: "from the string stream object in memory just as we would\nread "
17: "them from any other stream such as the standard input stream "
18: "or a file\nstream opened for text intput. To prove that we have "
19: "actually read the values\nas numerical quantities, we output "
20: "their average.";
21: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
23: string values("1.1 2.2 3.3 4.4 5.5 30.0");
24: istringstream inputStream(values);
25: double value;
26: double sum(0.0);
27: int valueCount(0);
28: while (inputStream >> value)
29: {
30: ++valueCount;
31: sum += value;
32: }
33: cout << "\nThe average of the " << valueCount << " values read is "
34: << sum / valueCount << ".";
35: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
36: }