-
Input Control
Hi all,
Can any one help me with the following program?
I want to make sure that user input is an integer. My code works reasonably well, except if user does not type anything and just hits enter, I want the program to capture it and says "INVALID INPUT, Try again..." or says "OUT OF RANGE, Try again..."
Thanks in advance. My compiler Borland C++.
//integer input control
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
int main()
{
do
{
int number=0;
unsigned int pass,chk;
char input[6]; //input control
clrscr();
do
{
cout<<"\t\t\t\tInteger Input Control"<<endl;
cout<<"\t\t\t\t====================="<<endl;
cout<<"Enter Integer Number (1 - 30000) ";
cin>>input;
pass=strlen(input);
//check if isdigit
for (chk=0;chk<pass;chk++)
{
if (!isdigit(input[chk]) || input[chk]=='£')
{
cout<<"INVALID INPUT! Try again...\n";
pass=0;
}
}
if (chk==pass)
{
number=atoi(input);
if (number<1 || number>30000 || pass>5)
{
cout<<"OUT OF RANGE! Try again...\n";
pass=0;
}
}
} while (pass==0);
cout<<"\nThanks, that's an integer number: "<<number;
cout<<"\n\nAny key for more, Esc to quit.";
} while (getch()!=27);
return(0);
}
-
I believe there's an STL function called isdigit, don't have a reference handy, but look for that.
-
I know there is an isdigit function but I cannot think of the header right now. Sorry.
-
you can indeed use isdigit(); it is included in the header file ctype.h and it returns a nonzero if the character is a digit, else it returns zero.
so
include <ctype.h>//required header
int data; //data u wish to check
if(isdigit(data))==0) {cout<<"Not a numeric entry!";}
-
Yeah, I am using isdigit headher file already, but what I want is to capture empty input from user, eg. if they don't type in anything, when they just hit enter, I want the program to say one of the two things "INVALID INPUT" or "OUT OF RANGE". At the moment if you just hit return key without typing anything, it just gives you a blank line. Any body can help or understand what I am trying to do? - Thanks all the same.
-
from my experience in c++, if while inputting a integer you press enter it will move to the next line and if you enter a char it will give an error
the 'empty input catching' might work for strings and characters.
-
I don't have a ref here but look in Ctype.h . I know there is things in there to check for no input.
~Barjor
-
there are many checking facilities included in ctype.h
you can check for control characters and punctuation characters.
-
I know there is a function that checks it, I had to use it once, but I haven't had a need for it in a long time so I forgot what the command is. I am 98% certain there is a function that does it.
Man.....if you weren't using numbers you could just use the keycodes...
-
Whoa!
with all these replies I'm still no where near solving my problem of checking for empty input. There must be a way to do it, can any body please give me the command or function to achieve this?? - thanks.
-
I think I know what u mean. Maybe two loops will do w/the first one checking if it is a digit and the second one checking if it is in range.
-
golfinguy4: I'm asking a 3rd checking for empty input, like pressing enter key without typing anything for the prompt to integer input? it seems to me nobody knows how this can be done. It's sad, very sad that such as this basic and simple requirement cannot be had easily. Come on expert coders show your real skills and true colours? - else I'll give up programming? - Thanks.