String stream in C++

Level 4207 keystrokes

Builds a string by writing mixed values into a stringstream, then parses three integers from another stringstream and prints their sum 60.

C++ code

// An in-memory stream that can be written to like cout
1stringstream ss;
// Numbers are converted to text as they are written
2ss << "Age: " << 25 << ", Score: " << 95.5;
// str() returns the accumulated text as a string
3string result = ss.str();
// Print Age: 25, Score: 95.5
4cout << result << endl;
5
// A stream that reads from the given string
6stringstream parser("10 20 30");
7int a, b, c;
// >> reads whitespace-separated numbers: a = 10, b = 20, c = 30
8parser >> a >> b >> c;
// Print the sum 60
9cout << a + b + c << endl;

Output

Age: 25, Score: 95.5
60
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 16
  • ;× 8
  • "× 6
  • >× 6
  • ,× 3
  • :× 2
  • .× 2
  • (× 2
  • )× 2
  • +× 2

More C++ examples at level 4

← C++ typing practice