-
Get input integer number
Can anyone give me a hand with such problem? I wrote a function to get input
only integers. The function accepts only digits, then transfers string into
integer number. But in the case if the user wants to change some digits,
using Backspace key it is not working. I would be appreciating for any help.
unsigned long get_prod_code(const unsigned long min, const unsigned long
max)
{
char string[MAX_LENGTH];
unsigned long a = 0;
int i =0,
cnum = 0;
do
{
while ((cnum = (char)getch()) != NEW_LINE )
{
if ( !isdigit(cnum) && cnum != 8)
continue;
else
putchar(cnum);
string[i++] = (char)cnum;
}
string[i] = NULL;
/* Test whether the entered number within
* min and max frame */
a = atol(string);
if (a < min || a > max)
{
INVALID_INPUT;
i = 0; /* Reset string */
string[i] = NULL;
PRINT_PROMPT;
}
else
break;
} while(1);
return(a);
}
-
Re: Get input integer number
Getch does not allow this. Use getline and check the result for not digits,
then as before...
"Bona" <sergiy@xtra.co.nz> wrote:
>
>Can anyone give me a hand with such problem? I wrote a function to get input
>only integers. The function accepts only digits, then transfers string into
>integer number. But in the case if the user wants to change some digits,
>using Backspace key it is not working. I would be appreciating for any help.
>
>
>
>unsigned long get_prod_code(const unsigned long min, const unsigned long
>max)
>{
> char string[MAX_LENGTH];
>
> unsigned long a = 0;
> int i =0,
> cnum = 0;
>
> do
> {
> while ((cnum = (char)getch()) != NEW_LINE )
> {
> if ( !isdigit(cnum) && cnum != 8)
> continue;
>
> else
> putchar(cnum);
>
> string[i++] = (char)cnum;
> }
>
> string[i] = NULL;
> /* Test whether the entered number within
> * min and max frame */
> a = atol(string);
>
> if (a < min || a > max)
> {
> INVALID_INPUT;
> i = 0; /* Reset string */
> string[i] = NULL;
> PRINT_PROMPT;
> }
> else
> break;
>
> } while(1);
>
> return(a);
>}
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks