troubleshooting Question

error trapping for non integers

Avatar of kanezy
kanezy asked on
Programming
5 Comments2 Solutions318 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 2 Answers and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros