C++

C++ Check If Input Is Int

C++ is a very versatile language when it comes to functions and utilities. Let’s say you want to check for value if it’s an integer or not. You may use more than 1 method to find that. Thus, we will take a look at those methods in this article to check if the specified value by a user is an integer or not.

Make sure to have a “G++” compiler for C++ language already configured and updated in your Ubuntu 20.04 Linux operating system. Let’s get started with the launch of the Ubuntu terminal using “Ctrl+Alt+T”. Create a C++ file using the simple “touch” instruction, and launch it within the “nano” editor of our Ubuntu 20.04 system as below.

Example 01:

We will start our C++ code within this file with the “iostream” header library and standard namespace, i.e., “Std” to utilize the code’s input and output statements, i.e., cout and cin. The C++ code execution always starts from its main() function.

Let’s discuss the main() function first. The main() function has been defined with “int” return type after the Boolean return type user-defined “check” function. The string “v” has been defined with no value. The “cout” statement is here to ask a user for a value, i.e., integer or string. The “cin” statement is here to save the value entered by a user in the variable “v”.

The “if-else” statement has been utilized to check whether the value entered by a user is an integer or not. Within the “if” part, we have been calling the Boolean “check()” function passing the variable “v” as an argument to it. The control goes to the “check” function. With the “check” function, the “for” loop has been used to iterate the value entered by a user for every letter/character until the end of the variable.

The “for” loop has the “if” statement again to use the “isdigit()” function on each character of value “v” entered by a user. The function “isdigit()” returns true or false in return. If it returns “true” it means a particular character is a number. Thus, the “return” statement will return “true” to the main() method. Else, it will return “false”.

The control came back to the main() function after the “for” loop ends. If the Boolean value returned by the “check” function is “true”, the “if” part cout statement will get executed. Else the “else” part cout statement will be executed.

#include<iostream>
using namespace std;
bool check(String v) {
  for (int i=0; i<v.length(); i++)
  if (isdigit(v[i]) == true)
      return true;
      return false;
}
int main() {
  string v;
  cout<<"Please enter some value: ";
  cin>>v;
  if(check(v))
      cout<<"The value "<<v<<" is Integer"<<endl;
  else
      cout<<"The value "<<v<<" is Not Integer"<<endl;
   }

Let’s use the g++ compiler to compile the code and run the “./a.out” command. The user inputs “hello” and get the message “value is not an integer”. The user inputted “140” as a value on the second execution and got the message “value is an integer”.

Example 02:

The isdigit() function can be utilized in another way within the code to check for the value if it is an integer or not. For this illustration, we will not be using the user-defined function check(). All the work will be managed within the main() function.

Within the main() method, we have initialized a variable “count” to “0” after the declaration of a string variable “v”. The cout statement has been used after that to ask the user for an input value. The cin statement is here to save the value entered by a user in a variable “v”. The “for” loop is used in the main() method as we did use it in the check() function before. It will iterate the value entered by a user up to its length.

The “if” statement is here to utilize the “isdigit” function. If the isdigit() value equals “true”, the count variable will be incremented. After the “For” loop, another “if” statement is here to check the “count” value and react according to that. If the cout value is 0 and equal to “length of the string”, it will display that the value is integer via the cout statement. Otherwise, the “else” statement will execute, showing that the value is not an integer.

#include<iostream>
using namespace std;
int main() {
  string v;
  int count=0;
  cout<<"Please enter some value: ";
  cin>>v;
  for (int i=0; i<v.length(); i++) {
  if (isdigit(v[i]) == true)
      count++;
}
  if(count>0)
      cout<<"The value "&lt<;v<<" is Integer"<<endl;
  else
      cout<<"The value "<<v<<" is Not Integer"<<endl;
   }

On compilation and execution of this code, we have entered the “hell” value first and found that it’s not an integer value. We entered “42” as a value on the second execution, and it displays that the “42” is an integer value.

Example 03:

The function “find_first_not_of()” is a built-in function of C++. The variable “v” has been checked out through the “find_first_not_of()” function. It says if any character from value “v” is other than “0123456789” until the end of the variable, it will return “string::npos” which means “not matched”.

If the function return value equals “true” and no character string found so far i.e., the first cout statement will display that the value is an integer. Otherwise, if any of the values are matched, it will display that the value is not an integer i.e. might be a string.

#include<iostream>
using namespace std;
int main() {
  string v;
  cout<<"Please enter some value: ";
  cin>>v;
  if((v.find_first_not_of("0123456789") == string::npos) = true)
      cout<<"The value "<<v<<" is Integer"<<endl;
  else
      cout<<"The value "<<v<<" is Not Integer"<<endl;
   }

On execution, the user added “c++” and the output shows the value is not an integer. On another execution, the user added 9808 and found that it is an integer value.

Conclusion:

This article covered some of the built-in utilities in C++ to check if the input value is some integer or not. Those utilized include the isdigit() function, count utility, Boolean values, and the find_first_not_of() function along with string::npos. Everything is well explained; thus, it will be quite easy to understand.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content