Convert string user input to int with sstream

The following example converts a string into an integer.

#include <iostream> 
#include <sstream>
#include <string>
#include <cstdio>

int main() 
    std::string s_year;
    int year;
    std::cout << "Please enter a year: ";

    // storing user input into the s_year variable 
    std::getline(std::cin, s_year);

    // converting to integer 
    std::stringstream(s_year) >> year;

    // printing using integer format
    printf("%d", year);


Notice that after coversion, we print the year variable using an integer format.

$ g++ main.cpp -std=c++17 -o string_to_int
$ ./string_to_int 
Please enter a year: 2000