# C++ String to Integer Conversion

**URL:** <https://forum.devtalk.com/t/c-string-to-integer-conversion/127888>\
**Category:** General Dev Chat\
**Tags:** programming\
**Created:** [10 October 2023 12:03 UTC](https://forum.devtalk.com/t/c-string-to-integer-conversion/127888 "2023-10-10T12:03:12Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![gulshan212](https://forum.devtalk.com/user_avatar/forum.devtalk.com/gulshan212/32/4971_2.png) [@gulshan212](https://forum.devtalk.com/u/gulshan212)\
**Post date:** [11 October 2023 17:19 UTC](https://forum.devtalk.com/t/c-string-to-integer-conversion/127888/3 "2023-10-11T17:19:11Z")

</div>

Well, I can see some logical issues with your code.  
Can you try this code and confirm wether it is working or not.

```auto
#include <iostream>
#include <string>
#include <stdexcept> // Include this for std::invalid_argument and std::out_of_range

int main() {
    std::string str = "12345"; // This could be any numeric string.

    int num = 0; // The converted integer will be stored here.

    try {
        num = std::stoi(str);
        std::cout << "Converted integer: " << num << std::endl;
    } catch (const std::invalid_argument& e) {
        std::cerr << "Invalid argument: " << e.what() << std::endl;
    } catch (const std::out_of_range& e) {
        std::cerr << "Out of range: " << e.what() << std::endl;
    }

    return 0;
}

```

Thanks

---

_[View the full topic](https://forum.devtalk.com/t/c-string-to-integer-conversion/127888)._
