How to Check if an Input Is a Number in C++
In this tutorial, we will see how to check if string is a number in C++.
Every application in C++ is designed for a unique purpose. And to fulfil these purposes we make use of the unique data types offered by the C++ programming language.
For example: Character, Boolean, Floating Point, Double Floating Point, Integer, String etc.
Each one of these data types occupy a specific amount of memory space, determining the size and the layout of the variable's memory map. To specify these data types in the program we make use of keywords. These variable data types help us to manipulate data however we please.
In this tutorial we will dive deep in two of these variable data types.
- String
- Integer
String
- A string variable carries a collection of characters, these characters may include alphabets/ letters or other characters.
- All the characters in string variable are always between double quotes.
Example: // Include the string library
#include <string> // Create a string variable string name of the blog = "Welcome to Java2Blog" ; |
Here, string variable is Welcome to Java2Blog
Integer
- An Integer carries a whole number value, like 25 or 23645.
- They have variable sizes.
- Integers can be signed or unsigned, depending upon whether they support negative or positive value.
Example:
#include <iostream> using namespace std ; int main ( ) { int myNum = 765 ; cout << myNum ; return 0 ; } |
Here, the integer is 765
Sometimes some applications need to identify whether a string is a number or not. There several ways to check if String is number in C++.
Below are the programs that can help users to identify if a string is a number.
Using std::isdigit() method to check if a string is number.
This is the most common method used for the solution of this problem.
In this method, we pass a string as a parameter to the isNumber()
function. It iterates over every character present in the string and check with isdigit method.
When it detects the first non-digit, the function returns false and displays message accordingly.
If not a single non-digit is found, the function returns true and the appropriate message is displayed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> using std :: cout ; using std :: cin ; using std :: endl ; using std :: string ; bool isNumber ( const string & s ) { for ( char const & ch : s ) { if ( std :: isdigit ( ch ) == 0 ) return false ; } return true ; } int main ( ) { string s1 = "Java2Blog" ; string s2 = "C++" ; string s3 = "5189746" ; isNumber ( s1 ) ? cout << " Java2Blog is a Number\n" : cout << "Java2Blog is Not a number\n" ; isNumber ( s2 ) ? cout << "C++ is a Number\n" : cout << "C++ is Not a number\n" ; isNumber ( s3 ) ? cout << "5189746 is a Number\n" : cout << "5189746 is Not a number\n" ; return 0 ; } |
Output:
Java2Blog is Not a number
C++ is Not a number
5189746 is a Number
Using find_first_not_of() method to check if a String Is a Number.
This program has an inbuilt algorithm to search for strings.
It searches all the strings for characters, if the character is not found, i.e. if a non-digit is not identified it returns string :: npos.
The result of all the checked strings is then displayed with the help of isNumber.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> using std :: cout ; using std :: endl ; using std :: string ; bool isNumber ( const string & s ) { return s . find_first_not_of ( "0123456789" ) == string :: npos ; } int main ( ) { string s1 = "Java2Blog" ; string s2 = "6767526" ; string s3 = "#$&&*" ; isNumber ( s1 ) ? cout << " Java2Blog is a Number\n" : cout << "Java2Blog is Not a number\n" ; isNumber ( s2 ) ? cout << "6767526 is a Number\n" : cout << "6767526 is Not a number\n" ; isNumber ( s3 ) ? cout << "#$&&* is a Number\n" : cout << "#$&&* is Not a number\n" ; return 0 ; } |
Output:
Java2Blog is Not a number
6767526 is a Number
#$&&* is Not a number
Using std::isdigit and std::all_of methods to check if String is a Number (C++11 onwards)
You can also use all_of()
with isdigit()
method to check if String is a Number or not.
all_of()
can save time to run a loop to check each items one by one. It checks for given property on each element one by one and returns true if all the elements satisfy the property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> #include <algorithm> using namespace std ; bool isNumber ( const std :: string & s ) { return ! s . empty ( ) && std :: all_of ( s . begin ( ) , s . end ( ) , :: isdigit ) ; } int main ( ) { string s1 = "Java" ; string s2 = "4572232" ; string s3 = "Python123" ; isNumber ( s1 ) ? cout << " Java is a Number\n" : cout << "Java is Not a number\n" ; isNumber ( s2 ) ? cout << "4572232 is a Number\n" : cout << "6767526 is Not a number\n" ; isNumber ( s3 ) ? cout << "Python123 is a Number\n" : cout << "Python123 is Not a number\n" ; return 0 ; } |
Output:
Java is Not a number
4572232 is a Number
Python123 is Not a number
Using std::isdigit and std::find_if methods to check if String is a Number (C++11 onwards)
You can also use find_if()
with isdigit()
method to check if String is a Number or not.
all_of()
can save time to run a loop to check each items one by one. It checks for given property on each element one by one and returns true if all the elements satisfy the property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <algorithm> using namespace std ; bool isNumber ( const std :: string & s ) { return ! s . empty ( ) && std :: find_if ( s . begin ( ) , s . end ( ) , [ ] ( unsigned char c ) { return ! std :: isdigit ( c ) ; } ) == s . end ( ) ; } int main ( ) { string s1 = "abc123" ; string s2 = "654343" ; string s3 = "m1232" ; isNumber ( s1 ) ? cout << " abc123 is a Number\n" : cout << "abc123 is Not a number\n" ; isNumber ( s2 ) ? cout << "654343 is a Number\n" : cout << "654343 is Not a number\n" ; isNumber ( s3 ) ? cout << "m1232 is a Number\n" : cout << "m1232 is Not a number\n" ; return 0 ; } |
Output:
abc123 is Not a number
654343 is a Number
m1232 is Not a number
The above foue methods show how to identify if a string is a number when the string to be identified was in the code itself. But many applications need to determine whether a string entered by the user is a number or not.
How to check if user input is number or not.
In this program, we will take an input from the user in a string named checkint
.
After initialization we calculate the length of the string and try to identify the first non-digit.
Depending upon the input the final message is displayed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <iostream> using namespace std ; int main ( ) { int i , count ; string checkint ; cout << "Enter a string : " ; cin >> checkint ; for ( i = 0 ; i < checkint . length ( ) ; i ++ ) { if ( isdigit ( checkint [ i ] ) == false ) { count = 1 ; break ; } else count = 0 ; } if ( count == 0 ) cout << "Is an Integer" ; else cout << "Not an Integer" ; } |
Output:
Enter a string : 32143
Is an Integer
These are the various methods by which a string can be easily identified as a number.
Uses
The operation of identifying a string as a number or not is used in many places. The most common example is Roll number system in schools/ collages. Where a student's roll number needs to be entered in the system. If the correct roll number (digits only) is entered, the system proceeds, otherwise it shows an error message.
How to Check if an Input Is a Number in C++
Source: https://java2blog.com/check-if-string-is-number-cpp/
0 Response to "How to Check if an Input Is a Number in C++"
Post a Comment