Check If Input is Numeric in C++



Numeric input means a value that contains only digits from 0 to 9, without any letters or special characters. In this article, we'll show you how to write a C++ program to check whether the input is numeric.

Let's understand this with a few examples:

//Example 1:
Input:
12345
Output:
Valid numeric input

//Example 2:
Input:
12a5
Output:
Not a numeric input

We will cover two common ways to check if the input is numeric or not in C++.

Checking Numeric Input Using std::getline with std::isdigit

In this approach, we read the input as a string and then check each character using std::isdigit to make sure every character is a digit. This method strictly validates integer inputs.

Example

Below is a complete C++ example that reads input as a string and checks each character to confirm it is numeric.

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

// Checking if the string contains only digits
bool isNumeric(const string& str) {
    for (char ch : str) {
        if (!isdigit(ch)) {
            return false;
        }
    }
    return true;
}

int main() {
    // First predefined input (numeric)
    string input1 = "4567";
    if (isNumeric(input1)) {
        cout << "The input is numeric: " << input1 << endl;
    } else {
        cout << "The input is not numeric." << endl;
    }

    // Second predefined input (non-numeric)
    string input2 = "45a7";
    if (isNumeric(input2)) {
        cout << "The input is numeric: " << input2 << endl;
    } else {
        cout << "The given input: " << input2<<" is not numeric." <<  endl;
    }
    return 0;
}

The output below shows the result of checking two predefined inputs for both numeric and non numeric.

The input is numeric: 4567
The given input: 45a7 is not numeric.

Time Complexity: O(n), where n is the length of the input string because the program checks each character once.

Space Complexity: O(1), as it uses only a few variables.

Checking Numeric Input Using stringstream

In this approach, we read the input as a string and use std::stringstream to convert it into a number. If the conversion is successful and no extra characters remain, the input is numeric.

Example

In this example, we use stringstream to convert the input string to an integer and check that no additional characters are left.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

bool isNumeric(const string& str) {
    stringstream ss(str);
    int number;
    char leftover;
    // Try to extract an integer from the stringstream
    if (ss >> number) {
        // Check if there are any leftover characters after the number
        if (!(ss >> leftover)) {
            return true;  // string is numeric
        }
    }
    return false;  // Not a numeric string
}

int main() {
    // Test inputs, you can check yours
    string input1 = "789";   
    string input2 = "78a9";

    // Check and print result for first input
    if (isNumeric(input1)) {
        cout << "The input is numeric: " << input1 << endl;
    } else {
        cout << "The given input: " << input2<<" is not numeric." <<  endl;
    }
    // Check and print result for second input
    if (isNumeric(input2)) {
        cout << "Valid numeric input: " << input2 << endl;
    } else {
       cout << "The given input: " << input2<<" is not numeric." <<  endl;
    }
    return 0;
}

The output below shows how the program uses stringstream to convert and check whether the input is numeric or not.

The input is numeric: 789
The given input: 78a9 is not numeric.

Time Complexity: O(n) because we check characters at most once.

Space Complexity: O(n) because stringstream internally stores a copy of the string.

Updated on: 2025-05-30T18:07:17+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started