
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to check if input is numeric in C++?
Here we will see how to check whether a given input is numeric string or a normal string. The numeric string will hold all characters that are in range 0 – 9. The solution is very simple, we will simply go through each characters one by one, and check whether it is numeric or not. If it is numeric, then point to the next, otherwise return false value.
Example
#include <iostream> using namespace std; bool isNumeric(string str) { for (int i = 0; i < str.length(); i++) if (isdigit(str[i]) == false) return false; //when one non numeric value is found, return false return true; } int main() { string str; cout << "Enter a string: "; cin >> str; if (isNumeric(str)) cout << "This is a Number" << endl; else cout << "This is not a number"; }
Output
Enter a string: 5687 This is a Number
Output
Enter a string: 584asS This is not a number
- Related Articles
- C++ Program to Check if a String is Numeric
- How to check if an input is an integer using C/C++?
- Swift Program to Check if a String is Numeric
- Java Program to Check if a String is Numeric
- Golang Program to Check if a String is Numeric
- Haskell Program to Check if a String is Numeric
- How to check if a variable has a numeric value in Perl?
- C++ Program to check if input is an integer or a string
- How to check if a unicode string contains only numeric characters in Python?
- How do I check if raw input is integer in Python 3?
- How to limit an HTML input box so that it only accepts numeric input?
- Check if input is a number or letter in JavaScript?
- How to check if a variable is NULL in C/C++?
- How to check if an input string contains a specified substring in JSP?
- Check if Input, Output and Error is redirected on the Console or not in C#

Advertisements