# error trapping for non integers

i was working on this code to trap an error when i enter a value other than an integer.i want to know how i may write for it to handle + and -ve operations as part of integer without giving an error and also give the input value wether it is a character.like give 'w' if i enter w instead of an integer and +2 if i enter it.right now i can get pure integers back
#include <iostream.h>
#include <string.h>

void readInt (int &, bool &);
void readInt (int &j, int low, int high);
void main () {
int i;
cout <<i <<' ' <<ok <<endl;
}

void readInt (int &j, bool &ok) {
//  returns an integer j inputted with error trapping
//  ok= was the operation successful
//       #include <string.h>

ok= true;
int digit;
char input [20];
cout<<"Enter integer ";
cin >>input;

j= 0;
for (int ctr= 0; ctr < strlen(input); ctr++) {
digit= int (input[ctr]) - int ('0');

if (digit >=0 && digit <=9)
j=j*10+digit;

else
ok= false;
} // for

void readInt (int &j, int low, int high) {
// repeatedly inputs an integer until it is between low and high, inclusive
// characters accidently entered indicates an unacceptable number
bool ok;
while((j<low)||(low>high))
SOLUTION
sunnycoder

membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
char *p;
strtol(input,&p,10);
ok = *p == '\0';
kanezy

i am getting this output if i enter something like  -78b6.how am i supposed to set up the output

Enter integer -78b6
b6b6-786 0
Press any key to continue

#include <iostream.h>
#include <string.h>
#include <stdlib.h>

void readInt (int &, bool &);
void readInt (int &j, int low, int high);
void main () {
int i;
cout <<i <<' ' <<ok <<endl;
}

void readInt (int &j, bool &ok) {
//  returns an integer j inputted with error trapping
//  ok= was the operation successful
//       #include <string.h>

ok= true;
int digit;
char input [20];
cout<<"Enter integer ";
cin >>input;

j= 0;
for (int ctr= 0; ctr < strlen(input); ctr++) {
digit= int (input[ctr]) - int ('0');

if (digit >=0 && digit <=9)
j=j*10+digit;

else{
ok= false;
char *p;
strtol(input,&p,10);
ok = *p == '\0';
cout<<p;
}
} // for

if(input[0]=='-')
cout<<'-';
if(input[0]==
'+')
cout<<'+';