Write a code for checking if a given input is an integer value or not.
Code
---------------------------------------------------------
#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: deve124
This is not a number
No comments:
Post a Comment