C++

C++ Atoi() Function

Today, we’ll examine one of the C++ atoi() functions and utilize the several real-world scenarios to show how and where to use the atoi() functions in C++ with clear explanations. First, let’s quickly review the fundamentals of the C++ programming language. We’ll also explore why we need the functions in C++ and what strings are.

As we all know, C++ is a reasonably simple and basic object-oriented programming language that provides the programs with a distinct structure that allows the content to be viewed inside the same program. We use the different datatypes to declare the variables, classes, functions, etc. So, we set the data type of these variables, functions, and classes. String is one of those datatypes which is used to print the character values in the output. There are many built-in string functions of C++ and the atoi() function is one of these string functions.

The atoi() function is the predefined string function that is used to convert the input string value to an integer value. The atoi() function in C++ quietly terminates the execution if somehow the input string is empty or contains any non-integer value without generating an error or exception. If we have an input string “54123” and we pass this string value to the atoi() function, we get 54123 in return. But if we pass the decimal value “5745.001” into the atoi() function, we get 5745 in return. This is because the atoi() function is used to convert the string value into the integer value only.

Syntax:

Let’s examine the predetermined formatting and implementation of the string atoi() method which is used to accept a string as an argument and returns a string value that is converted to an integer value.

In this prototype, we first write the datatype function in which the function is returned in “int” datatype. After this, we create the name of the function which we want to implement, that is atoi() function, which is used to convert the string value to an integer value. Next, we open the atoi() function brackets and pass an argument in it so that we get the desired output. But as you can see in the previous discussion in the syntax, there is a “const” keyword in function brackets which is used because it informs the translator to stop the programmer from altering the value of the dependent variable(input_string) and declares that a variable’s value is constant. Then, we pass the input string into it.

Parameter:

input_string: The argument in the atoi() function which is used to get the input from the user in string datatype.

Return Value:

In return, we get the input value in an integer datatype by implementing the atoi() function.

Example 1:

Let’s just begin in writing the very simple example of a C++ string function that is used to convert the input string to an integer value called the string atoi() function. To start writing and executing the code, we first need a C++ compiler because we are writing a C++ program. Open the C++ compiler and begin writing the code that we wish to implement in the C++ programming language. Therefore, assuming that it is appropriate with the modules that you intend to utilize in the program, you can install any C++ compiler or use the online translator to create and run the source code.

#include <iostream>

#include <string>

using namespace std;

int main()
{
  char str[20];

  cout <>str;

  cout << "The value after the conversion of input string to an integer value: "  << atoi(str);

  return 0;
}

To start our program’s execution, we always include the basic modules in the program so that the functionality that we use is easily accessible. Every module of C++ has its different functionality. First, we include the “iostream” module which is used to get the input from the user. We can also display the output to the user in it. When we import a module into the program, we always write the “#” sign at the start of the module. So, we instruct the compiler that we access the module.

Then, we use the “include” keyword which is already defined in the C++ compiler. It is used to include any module in the C++ program. Then, we pass the name of the module which we want to implement in “<>” which is “iostream”. Next, we add the second module, “cstdlib”, which is a general purpose module that is used to implement the conversion function like the atoi() function. We can also do mathematical operations in it. We include this module using the same method that we previously used. Next, the "using namespace std" constraint is also used to restrict the remainder of the program's objects, methods, and parameters from utilizing the same domain.

Then, we start the main() function to write the actual line of code. First, we declare the character type variable “str[20]” of length “20”. To initialize the value to it, we use the cin() method so that we can get the input from the user. To print the input, we call the cout() method and then pass the variable in it. Then, we implement the main functionality that we want to implement in this code which is the atoi() function. In the atoi() function, we pass the input variable. Then, we print this using the cout() method. We show the output to the user.

Here is the output of the previously-implemented program. The user inputs the string value. And in return, we get the integer value.

Let’s see another scenario of the program: What if the user inputs the floating-point value as an input value? As you see in the following image, the compiler takes the value before the point as an integer value.

Example 2:

Now, we implement another example of the atoi() function. First, we include the libraries. We will do the same method which we used in the previously-implemented example. In the second library, we use a different method from the first library. Here, we use the “string” library so that we can easily use the string values and methods in the program. Now, we write the “namespace std” in the program.

#include <iostream>

#include <string>

using namespace std;

Next, we start the main() function. Here, we declare a character type variable “str” by assigning the “null” value to it. We declare one more variable which is “value” of type integer with the ”0” value. Then, we write the string value in double quotation marks and pass this value to the atoi() function and display it using the cout() method. We do the same but we initialize the floating-point value to the string variable this time. In the next function call, we initialize the alphanumeric value to the string variable. In the end, we return 0 to the main() function.

 int main( void )
{
    char *str = NULL;
    int value = 0;

    str = "-2309";
    value = atoi( str );
    cout << "String Vaule: " << str <<endl;
    cout << "IntegerValue: " <<  value<< endl << endl;

    str = ".8734";
    value = atoi( str );
    cout << "String Vaule: " << str <<endl;
    cout << "IntegerValue: " <<  value<< endl << endl;

    str = "987NJEAC98";
    value = atoi( str );
    cout << "String Vaule: " << str <<endl;
    cout << "IntegerValue: " <<  value<< endl << endl;

    return 0;
}

Here is the output of the previous illustration which we explained:

Conclusion

In this article, we learned about the string function which is the atoi() function. We also learned the writing style and implementation of the function with a detailed explanation so that the user can understand every line of code easily. We also learned some basics of the C++ language and string.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.