chevron_left
2,504 points
20 16 12

In C ++ language, after reading strings, there are problems to read numbers, because our program does not identify that they are integers and therefore operations cannot be performed.

To solve this it is necessary to convert our string to an integer.

#include <iostream>

#include <cstdlib>

using namespace std;

/ *

* Program that converts a string to an int integer and adds 2 to it

* /

int main ()

{

// Declaration of variables in string

string number;

// request and save the values in number

cout << "what is the number?" << endl;

cin >> number;

// convert your string to an integer add 2 to it and show you the result

cout << "Your number is" << atoi (number.c_str ()) + 2;

return 0;

}

Solution.

To convert our string to an integer we use the atoi function . This function returns the converted value.

It parses the string type string and interprets its content as an integer, which is returned as a value of type int.

The first function discards all empty spaces until it finds the first character. From this character it takes an initial value followed by base 10 digits as possible and interprets them as a numeric value.

The string can contain additional characters or text after those that form the integer, these characters are ignored and have no effect on this function.

If the first sequence of characters is not blank, str is not a valid integer, or if there is no such sequence, either because str is empty or only contains blanks, a value of zero is returned.

The above describes the following, for example: If we save a d24 in our string type string, what our atoi function will return is a 0, this because our first character is a letter. But if we save a 24d, what will be returned will be a 24, ignoring the d at the end.

To use the atoi function we need to call the c

#include <iostream>

#include <cstdlib>

using namespace std;

/ *

* Program that converts a string to an int integer and adds 2 to it

* /

int main ()

{

// Declaration of variables in string

string number;

// request and save the values in number

cout << "what is the number?" << endl;

cin >> number;

// convert your string to an integer add 2 to it and show you the result

cout << "Your number is" << atoi (number.c_str ()) + 2;

return 0;

}

Solution.

To convert our string to an integer we use the atoi function . This function returns the converted value.

It parses the string type string and interprets its content as an integer, which is returned as a value of type int.

The first function discards all empty spaces until it finds the first character. From this character it takes an initial value followed by base 10 digits as possible and interprets them as a numeric value.

The string can contain additional characters or text after those that form the integer, these characters are ignored and have no effect on this function.

If the first sequence of characters is not blank, str is not a valid integer, or if there is no such sequence, either because str is empty or only contains blanks, a value of zero is returned.

The above describes the following, for example: If we save a d24 in our string type string, what our atoi function will return is a 0, this because our first character is a letter. But if we save a 24d, what will be returned will be a 24, ignoring the d at the end.

To use the atoi function we need to call the cstdlib #include <cstdlib> library. And the syntax is as follows.

 

atoi(variableString.c_str());

stdlib #include <cstdlib> library. And the syntax is as follows.


atoi(variableString.c_str());

 

More Posts

How to convert integer to string in c++(In c++03) amna - Oct 19, 2020
How to convert string array to integer array in java Jeevani - Jun 30
[Java] How to Convert from Integer to String kennylim - Aug 11
(Java vs Javascript) - Convert String to an Integer in Java and Javascript DivyaN - Aug 8
Convert String to Integer with leading zeros DivyaN - Aug 8
Convert string to integer in C using strtol amna - Oct 4, 2020
How to convert int to string in JAVA MallikaShaik672 - Oct 21, 2020
Concatenate string and int c++ Naymul01 - 6 days
Concatenate string and int c++ sam_dev - Sep 21
Converting integer to string in Python yaroslavkushnir - Oct 6