Link to home
Start Free TrialLog in
Avatar of kanezy
kanezy

asked on

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
SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

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

ASKER

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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>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 :-)