Home » how to Convert a string to int in C++ ?

how to Convert a string to int in C++ ?

Today, We want to share with you string to int c++.In this post we will show you convert string to int c++ , hear for int to strings c++ we will give you demo and example for implement.In this post, we will learn about convert char to string c++ with an example.

c++ string, string to int, string c++, convert string to int, int to string c++
string to int c++

1. std::stoi

The standard approach is to use std::stoi methods for converting a string to an integer. stoi was additionals in C++11 and is defined in the main header <string>. This throws std::invalid_argument or std::out_of_range exception on a bad input or integer overflow respectively. This is worth noting that it will convert the strings for example 10xyz to integer 10.

#include <iostream>
#include <string>

int main() {

	std::string myStr = "10";

	try
	{
		int idxflg = std::stoi(myStr);
		std::cout << idxflg << '\n';
	}
	catch (std::invalid_argument const &e)
	{
		std::cout << "Bad input: std::invalid_argument thrown" << '\n';
	}
	catch (std::out_of_range const &e)
	{
		std::cout << "Integer overflow: std::out_of_range thrown" << '\n';
	}

	return 0;
}

2. string streams

std::stringstream can be used to convert std::string to more data types with inversely. This agonize from same issue as std::stoi did sample example it will data convert the strings for example 10xyz to integer 10. This returns INT_MAX or INT_MIN if the transformed value is out of range of integer data type. If the value of the string can’t be indicated as an int, 0 is returned.

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

int main() {

	std::string myStr = "10";

	int idxflg;
	std::istringstream(myStr) >> idxflg;

	std::cout << idxflg << '\n';

	return 0;
}

3. boost::lexical_cast

and then again best example is to use boost::lexical_cast. This throws boost::bad_lexical_cast exception when no valid integer could be constructed or overflow happens.

Read Also:  Laravel Eloquent Relationships Join Multiple Tables

#include <iostream>
#include <boost/lexical_cast.hpp>
#include <string>

int main() {

	std::string myStr = "10";
	int idxflg;

	try {
		idxflg = boost::lexical_cast<int>(myStr);
		std::cout << idxflg << '\n';
	}
	catch (boost::bad_lexical_cast const &e)	// bad input
	{
		std::cout << "error" << '\n';
	}

	return 0;
}

4. std::atoi

I can also use the std::atoi methods to convert a string to an int. This takes a c-string as argument so we required to first convert our string to a c-string. I can do that using std::string::c_str.

Please note that it’s ways is undefined if the transformed value is out of range of integer data type. If the data value of the main string can’t be indicated as an int, 0 is returned.

#include <iostream>
#include <string>
#include <cstdlib>

int main() {

	std::string myStr = "10";
	int idxflg = std::atoi(myStr.c_str());

	std::cout << idxflg << '\n';

	return 0;
}

5. sscanf()

I can pass a c-string to the sscanf() methods as a argument as well as convert it into corresponding data type as specified in the format specifier. This would return number of arguments on success as well as EOF on failure.

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

int main() {

	std::string myStr = "10";
	int idxflg;

	if (sscanf(myStr.c_str(), "%d", &idxflg) == 1) {
		std::cout << idxflg << '\n';
	} else {
		std::cout << "Bad Input";
	}

	return 0;
}

6. Range based for loop

Last step to, uniq and relavnts answer is to implement our own routine. The main Logic is to iterate through chars of the string using Range-based for loop (C++11) as well as process each digit individually. The way is undefined if the transformed value is out of range of integer data type.

#include <iostream>
#include <string>

int main()
{
	std::string myStr = "10";

	int idxflg = 0;
	for (char c: myStr)
	{
		if (c >= '0' && c <= '9') {
			idxflg = idxflg * 10 + (c - '0');
		}
		else {
			std::cout << "Bad Input";
			return 1;
		}
	}

	std::cout << idxflg << '\n';

	return 0;
}

I hope you get an idea about string to int c++.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Multiple File Upload in laravel 7 Example Tutorial

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  wordpress Get All Parent Categories from Child Categories
  2. Read Also:  How to Make Async Requests in PHP
  3. Read Also:  how to convert string to char c++ ?
  4. Read Also:  Difference between two dates in years, months, days in PHP
  5. Read Also:  HTML Form Enctype Multipart Attribute with Examples
  6. Read Also:  Laravel Roles Permissions Example Tutorial From Scratch
  7. Read Also:  oops concepts in php with realtime examples (Classes and Objects)
  8. Read Also:  Class 'App\Providers\View' not found in Laravel
  9. Read Also:  PHP OOP CRUD MVC Framework Tutorial
  10. Read Also:  how to convert string to char c++ ?

Leave a Comment

Your email address will not be published. Required fields are marked *