Day 16: Exceptions - String to Integer

Problem Link

Solution

StoI.cpp
#include <bits/stdc++.h>

using namespace std;

int main()
{
    string S;   
    cin >> S;
    
    int n;

    try
    {
        n=stoi(S);
        cout<<n<<endl;
    }
    catch(exception e)
    {
        cout<<"Bad String";
    }
    return 0;
}

Last updated

Was this helpful?