xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to convert string type to integer or double type in C++
C++ Online Editor
Execute Code
More C++ Sample Code
#include <iostream> #include <string> using namespace std; // use std namespace to avoid std:: before cout int main() { // Convert string to integer string stringVariable = "1225"; int outputInteger = stoi(stringVariable); cout << outputInteger + 25 << endl; // 25 added to the integer. Output will be 1250 // Convert string to double string stringNewVariable = "1225.25"; double outputDouble = stod(stringNewVariable); cout << outputDouble + 12.46 << endl; // 12.46 added to the double. Output will be 1237.71 // Another example string stringVariable2 = "T1225"; int outputInteger2; try { outputInteger2 = stoi(stringVariable2); cout << outputInteger2 + 25 << endl; } catch (const std::invalid_argument& e) { cout << stringVariable2 << " cannot be converted to integer." << endl; } return 0; }