
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to check if an input is an integer using C/C++?
Here we will see how to check whether a given input is integer string or a normal string. The integer 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 input is an integer or a string
- How to check if a variable is an integer in JavaScript?
- How to check if an array contains integer values in JavaScript ?
- How do I check if raw input is integer in Python 3?
- How do I check to see if a value is an integer in MySQL?
- How to check if input is numeric in C++?
- How to check multiple regex patterns against an input? Using Java.
- How to check if a C/C++ string is an int?
- How to check if an alert exists using WebDriver?
- How to check if an input string contains a specified substring in JSP?
- How to check if an URL is valid or not using Java?
- How to check if an image is displayed on page using Selenium?
- C Program to check if an array is palindrome or not using Recursion
- $n^2 - 1$ is divisible by 8, if $n$ is(A) an integer(B) a natural number(C) an odd integer(D) an even integer
- Program to check if an Array is Palindrome or not using STL in C++

Advertisements