TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: John Piper
from: Frank Adam
date: 1996-05-01 22:32:00
subject: Prob with user input

G'Day John,
 
-=> Quoting John Piper to All <=-

 JP> I am currently working on a small program which involves 
 JP> the user entering a number which will be used in a small 
 JP> mathematical calculation. However, I do
 JP> NOT want the user to be able to type a letter or punctuation
 JP> character. Such characters should be ingnored and not echoed to the
 JP> screen. 
Didn't mention if you want to allow negative values or not, so i will :)

#include
#include
#include
#include

int getdigit();
int getkey();

main()
{
 int num = getdigit();
 printf("\nReceived %d",num);
 return 0;
 }

int getdigit()
{
 int key = 0,count = 0,maxlen = 5;
 char numstr[7];

 printf("\nEnter number (-32768 to 32767) : ");

 while(key != 13)
 {
  key = getkey();
  
  if(isdigit(key) && count < maxlen)
  {
   putchar(key);
   numstr[count++] = key;
   }
   
  else if(key == '-' && count == 0)
  {
   numstr[count++] = key; /* only allow - as the first char */
   putchar(key);
   maxlen = 6;     /* have to adjust the maxlen for '-' + 5 chars */
   }
  }

numstr[count] = '\0';
return atoi(numstr);
}


int getkey()
{
 int key,lbyte,hbyte;
 union REGS regs;
 regs.h.ah = 0x10;
 int86(0x16, ®s, ®s);
 key = regs.x.ax;
 hbyte = (key&0xff00) >> 8;
 lbyte = key&0x00ff;
 if (lbyte == 0 || lbyte == 224) return hbyte+256;
 return lbyte;
 }

  L8r Frank (fadam{at}ozemail.com.au).
  

--- Maximus 3.01
* Origin: The Software Parlour (3:635/544)
SEEN-BY: 50/99 78/0 620/243 623/630 632/349 635/503 544 727 711/401 409 410
SEEN-BY: 711/413 430 808 809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 635/544 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.