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);
}