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
Programming

Avatar of undefined
Last Comment
sunnycoder

8/22/2022 - Mon
SOLUTION
sunnycoder

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ozo

char *p;
strtol(input,&p,10);
ok = *p == '\0';
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
ozo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sunnycoder

>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 :-)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy