C++ – How to check symbols in C++

cif statement

I'm beginner in programming (specially in C++). I've tried to check incoming symbols for letters. I need to "catch" only numbers from 0 to 9. So, i try to use it:

// Checking "a". If "a" is a letter, the error must be printed;
if (a!='%d') {
    cout << "pfff! U cant use letters!" << endl;
    return -1;
}
// The end of checking;

But it doesn't work. I guess that i can't use '%d' in C++. How i can say:
"Check all symbols and stop the program if there will be non-numbers."

P.S. Sorry for my english. I hope u got me.

Best Answer

Yes , isdigit() will work out nicely here .

An example is :

    #inlude <iostream>
    #include <ctype.h>
    // in ctype.h is located the isdigit() function
    using namespace std;
    //...
    char test;
    int x;
    cin >> test;
    if ( isdigit(test) )//tests wether test is == to '0', '1', '2' ...
    {
          cin>>putback(test);//puts test back to the stream
          cin >> x;
    }
    else
         errorMsg();