Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
Samual Sam

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.