C++ cstdlib atoi() Function
Example
Read the number from the text "24 degrees", and add 10 to it:
char text[] = "24 degrees";
int temp = atoi(text);
cout << temp + 10;
Definition and Usage
The atoi() function reads a whole number at the start of a C-style string (text stored in a char array), and returns it as an int.
It skips spaces at the start, and stops at the first character that is not a digit, like a space, a letter or a decimal point. If the text does not start with a number, it returns 0.
The atoi function is defined in the <cstdlib> header file.
Tip: To use atoi() with a string, write atoi(myText.c_str()). You can also use stoi(), see Convert Strings and Numbers.
Syntax
int atoi(const char* str);
Parameter Values
| Parameter | Description |
|---|---|
| str | Required. The C-style string to read the number from. |
Technical Details
| Returns: | An int with the number. If no number is found, it returns 0. |
|---|