C++

Isnumber in C++

The “isnumber()” is the function provided by the C++ language that aids in checking whether the entered data contains a number. Unfortunately, this “isnumber()” function is just for mac or Apple users. But the C++ language also facilitates us with the “isdigit()” function which acts the same as the “isnumber()” function. Both functions help in finding the number in the entered data. We can also say that “isdigit()” is the alternate of the “isnumber()” function. Here, we will explore the “isdigit()” function as we don’t have mac, and both functions are similar.

Example 1:

We require the “iosream” header file in this code. So, we include it here as it contains the function definitions of the “cin\cout” function. Underneath this, the standard namespace “std” is added. We invoke the “main()” method which is the driver’s code. Now, we first print a line with the help of the “cout”. Then, in the following “cout”, we utilize the “isdigit()” function in which we pass “2” as the parameter. Now, this “isdigit()” function checks whether “2” is the digit or not. If “2” is a digit, it returns “1”. If it is not, “0” is the outcome of this function.

Code 1:

#include <iostream>
using namespace std;
int main() {
  cout << "The value is digit or not: ";
  cout << isdigit('2');
  return 0;
}

 

Output:

This outcome renders “1” which means that the data that we previously entered is the digit or number. So, this “isdigit()” function returns “1”.

Example 2:

Three header files are included in this instance as we have to utilize the defined functions. We import the “cctype” and “iostream” as well as the “cstring” header file. The “cctype” header file is included as it offers the character testing and manipulation functions. The “iostream” header file is included as it offers the input and output functions, and the “cstring” is included as it offers the function that we apply to the strings in our code.

Now, the “std” and the “main()” methods are invoked. Then, the character array is initialized where we enter the string data, including numbers. The “strDigit[]” is the array of “char” here. Then, underneath this, we declare the “result” of the “int” data type. The “cout” renders the given text. Then, we place the “for” loop there from which the characters of the string are passed one by one. Then, the “isdigit()” function, which we utilize after this, checks if the string’s character is digit or not. If it is a digit, it is saved in the “result” variable as we initialize this “result” variable with the “isdigit()” function. Then, we place the “if” and pass the “result” as the argument and then display the number with the aid of ”cout”.

Code 2:

#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
  char strDigit[] = "azz2@mp;1p8$.;qr";
  int result;
  cout << "The string contains the following digits:" << endl;
  for (int a = 0; a < strlen(strDigit); a++)  {
    result = isdigit(strDigit[a]);
    if (result)
      cout << strDigit[a] << endl;
  }

  return 0;
}

 

Output:

The string that we previously added contains three numbers which are rendered here. We got these numbers from the string with the aid of the “isdigit()” function.

Example 3:

Three header files are included in this case since we need to use the provided functions. Here, we import the header files for “cctype”, “iostream”, and “cstring”. The “std” namespace is added and the “main()” function is now called. After that, the character array, into which we insert the string data, is initialized. This includes integers. The array of “char” in this case is called “s[]”. We define the “digits” of the “int” data type underneath it.

The specified text is rendered by the “cout” and the “for” loop is then inserted from which the string’s characters are fed individually. The “isdigit()” function is then used to determine whether or not the string’s characters are digits. Since we initialize the “digits” variable using the “isdigit()” function, if it is a digit, it is saved in the “digits” variable. Next, we insert the “if” statement, pass the “digits” as an argument, and use the “cout” to display the number.

Code 3:

#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
  char s[] = "My have 3 brothers, 4 sisters, 7 uncles, 9 aunt";
  int digits;
  cout << "This sentence contains digits :" << endl;
  for (int i = 0; i < strlen(s); i++)  {
    digits = isdigit(s[i]);
    if (digits)
      cout << s[i] << endl;
  }
  return 0;
}

 

Output:

The four digits in the string that we previously added are displayed in the following. The “isdigit()” method lets us extract the numbers from the string.

Example 4:

The “cctype” and “iostream” are added as these header files are required in this code. Then, we place the standard “std” namespace here. Now, we call the “main()” method. After this, we initialize four variables of the “char” data type with the names “character_1”, “character_2”, “character_3”, and “character_4” with the values of “z”, “2”, “5”, and “&”, respectively.

Then, we check all characters with the help of the “isdigit()” function and place these variables separately in this function after adding the “cout”. If the given character is a number, it renders “1” there. Otherwise, it renders “0”. If we enter the character or alphabet, the outcome of the “isdigit()” function is “0”.

Code 4:

#include <cctype>
#include <iostream>
using namespace std;
int main()
{
  char character_1 = 'z';
  char character_2 = '2';  
  char character_3 = '5';
  char character_4 = '&';
  cout<<character_1<<" is a digit: "<<isdigit(character_1)<<endl;
  cout<<character_2<<" is a digit: "<<isdigit(character_2)<<endl;
  cout<<character_3<<" is a digit: "<<isdigit(character_3)<<endl;
  cout<<character_4<<" is a digit: "<<isdigit(character_4)<<endl;
  return 0;
}

 

Output:

Now, this outcome renders “1” where the “isdigit()” function finds the number and renders “0” where it finds the special character or alphabet as shown in the following:

Example 5:

Here, we declare the “character” variable and then display a line that tells the user to enter the data. Underneath this, we place the “cin” which accepts the user’s input and save it in the “character” variable.

Below this, we place the “isdigit()” function and pass the “character” variable in this function that checks whether the entered character is a digit or not. Then, we render the first “cout” statement that we typed here. If is not a digit, the second “cout” statement is displayed. Here, we get the user’s input and then apply the “isdigit()” function to the user’s input data.

Code 5:

#include <cctype>
#include <iostream>
using namespace std;
int main ()
{
  char character;
  cout << "Enter data here which you want to check" <> character;
  isdigit (character)? cout << "This is a digit."  
  : cout << "This is not a digit";
  return 0;
}

 

Output:

When we enter the alphabet which is “p”, it displays the second statement which says “This is not a digit” as the outcome of the “isdigit()” function.

Now, we execute the previous code again. This time, we enter “9” here and hit “Enter”. Then, it renders the first statement which says, “This is a digit”, as the result of the “isdigit()” function.

The previous code is run once more, but we insert “@” this time and press “Enter”. As a result, the second statement, “This is not a digit”, appears because of the “isdigit()” function.

Conclusion

We defined in this article that “isnumber()” and “isdigit()” are the same functions in C++ programming. The difference is that the “isnumber()” is for mac users, so we utilize the “isdigit()” function in place of the “isnumber()” function in C++ programming. We thoroughly explored that this function aids in checking the number from the entered data and rendering the outcome accordingly.

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.