std::stoi Convert String To Numbers in C++ [3 Ways]

In this article, You will learn how you can convert String to Numbers in C++. In C++, we have a method named std::stoi that can be used to convert strings consisting of numbers to actual numbers or integers.

Using Method std::stoi Function To convert String to Numbers

In C++ standard library there is a method inbuilt created to convert the string to numbers. This function was first introduced in C++ 11, so make sure that you have or running a higher version of C++ to utilize this function.

In the below code we will see various examples of strings that are converted into numbers or integers. Also, you will see the example for exceptions that you can experience such as std::invalid_argument and std::out_of_range based upon the incorrect input types.

#include<bits/stdc++.h>
using namespace std;

void printNumber(string &example){

    int convertedInteger = 0;

    try
    {
        convertedInteger = std::stoi(example);
        cout<<convertedInteger<<endl;
    }//Catching the Exception Invalid Argument and Out of Range
    catch(const std::invalid_argument & e){
        std::cerr << "Invalid Input" << '\n';
    }catch(const std::out_of_range &e){
        std::cerr<< "Input Out Of Range" << '\n';
    }
}
 
int main(){

    //Initializing Number of Example String to convert to Numbers
    string example1 = "10";
    string example2 = "52356 hgsdf";
    string example3 = "472abc";
    string example4 = "3254983724598";
    string example5 = "andas546";

    //printing the string converted to numbers 
    // Using std::stoi method

    printNumber(example1);
    printNumber(example2);
    printNumber(example3);
    printNumber(example4);
    printNumber(example5);



    return 0;         
}

Output:

10
52356
472
Input Out Of Range
Invalid Input

Alternatively Using stringstream Method

You can convert the string to a number easily using the method stringstream. In the below code we will modify the above code and check what output will be received for the above inputs using stringstream.

Note in the above function we received errors for two examples that are taken care of in this class of stringstream. Instead of throwing an exception if the string cannot be converted stringstream will simply return 0 for it.

#include<bits/stdc++.h>
#include<sstream>
using namespace std;

void printNumber(string &example){

    //Creating A Object of stringstream 
    stringstream streamObject(example);

    int convertedInteger = 0;

    //Overloading Convert Integer to integer variable
    streamObject>>convertedInteger;

    cout<<convertedInteger<<endl;
}
 
int main(){

    //Initializing Number of Example String to convert to Numbers
    string example1 = "10";
    string example2 = "52356 hgsdf";
    string example3 = "472abc";
    string example4 = "3254983724598";
    string example5 = "andas546";
    string example6 = "abc214367dfs";

    //printing the string converted to numbers 
    // Using std::stoi method

    printNumber(example1);
    printNumber(example2);
    printNumber(example3);
    printNumber(example4);
    printNumber(example5);
    printNumber(example6);



    return 0;         
}

Output:

Convert String To Numbers in C++ [stoi]

Alternatively Using std::atoi Method

std::atoi function can also be used to convert the string to numbers. In a byte string pointed to by str, interprets an integer value using atoi. This method will discard any whitespace characters till a non-whitespace character is found.

The plus or minus sign, as well as the numeric digits, make up a valid integer value. This method will return 0 if it is unable to convert the input string. Let us see this method in the actual code below and see how it performs on a given input.

#include<bits/stdc++.h>
using namespace std;

void printNumber(const char *example){

    int convertedInteger = 0;

    convertedInteger = std::atoi(example);
    cout<<convertedInteger<<endl;

}
 
int main(){

    //Initializing Number of Example String to convert to Numbers
    const char* example1 = "10";
    const char* example2 = "52356 hgsdf";
    const char* example3 = "472abc";
    const char* example4 = "3254983724598";
    const char* example5 = "andas546";

    //printing the string converted to numbers 
    // Using std::atoi method

    printNumber(example1);
    printNumber(example2);
    printNumber(example3);
    printNumber(example4);
    printNumber(example5);



    return 0;         
}

Output:

10
52356
472
-601485770
0

That is all for this post on how to convert string to numbers in C++. As you already learned that this can be achieved using method std::stoi[1].

Then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Also, subscribe to our newsletter to get notifications for the new posts.

Further Read:

  1. How To Pop Front in Vector C++ [pop_front]
Share your love

One comment

Leave a Reply