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;
      bool ok;//readInt (i ,67,100);
      readInt (i, ok);
      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
} // readInt

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))
readInt(j,ok);
} // readInt
kanezyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Hi kanezy,

Use strtol function to convert your strings to integer .. It will  automatically handle + and - signs for you. Also it has a parameter called endptr that will tell you what was the first non digit character which it enountered during conversion. To get values out of characters, you can use this endptr.
Refer to help page or this link for more information
http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_22024988.html

Cheers!
sunnycoder
ozoCommented:
char *p;
strtol(input,&p,10);
ok = *p == '\0';
kanezyAuthor Commented:
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;
      bool ok;//readInt (i ,67,100);
      readInt (i, ok);
      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<<'+';
} // readInt

ozoCommented:
    char input [20];
     cout<<"Enter integer ";
     cin >>input;
                    char *p;
                    digit = strtol(input,&p,10);
                    ok = *p == '\0';
                    cout<<digit;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunnycoderCommented:
>how am i supposed to set up the output
>Enter integer -78b6
>b6b6-786 0
How did you calcuate output to be "b6b6-786 0"

If you convert sting -78b6 to integer using strtol, the ouput would be -78 .. Conversion stops at first non-digit character.

If you want different behaviour, strtol cannot help and we will have to develop a custom routine. But first, what is the expected behavior :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.