Lesson 17: Data validation / Type checking with StringStream

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)

[spoiler title=”Lesson Source Code”]

#include <iostream>
#include <string>
#include <sstream> //If you type this and get an error your stuff is too old

using namespace std;
int main(){

  string str = "";
  int value = 0;
  bool cont = true;
  while (cont){
    cout << "Enter a value: ";
    getline(cin, str);
    stringstream ss(str);
    if (ss >> value && value >5 ){
      cont = false;

  cout << endl;
  return 0;


Homework: None

What is StringStream?
StringStream is a library that is used to convert from strings to numeric types in C++. StringStream models it’s usage behaviors after iostream so it’s easy to learn for people that are familiar with the basics of C++. StringStream replaces older methods of doing this like atoi or itoa and gives a much safer, error-free method of handling data parsing in C++.