Jump to content

Recommended Posts

Posted

Does anyone have Niemand's hack-snippet for getting a numeric response from the user? I'd like to use it instead of my current "loop to check every possibility" implementation so that I can allow larger numbers to be input; I know it's on the Blades Forge, but that has been taken down.

Posted

I think that this was the final version, but I've just dredged it up from the script where I was originally trying it out, so you should probably look it over just to make sure it does what it should.

 

Code:
	get_text_response("Enter a number:");		b = 0; // parse status: 0=good, 1=good+negative, 2=format error, 3=range error	if(get_flag(299,29) == 1 || get_flag(299,29) == 2){ //On Mac OS		a = 0; // will hold the result value		i = 0; // counter		while(i<32 && b<2){ //limit input to 32 characters			//read one character			if(get_flag(299,29) == 1)				j = get_floor(-6140,i - 32);			else if(get_flag(299,29) == 2)				j = get_floor(-6118,i + 46);			//parse			if(j == 0) //NUL termination				i = 1000;			else if(i==0 && j==45) //first character may be a negative sign				b = 1;			else if(j>47 && j<58){ //a digit				if((b==0 && (a>3276 || (a==3276 && j>55))) || (b==1 && (a>3276 || (a==3276 && j>56))))					b = 3;				else					a = (10 * a) + (j - 48);			}			else //error				b = 2;			i = i + 1;		}		if(i==32 && j!=0)			b = 2;		if(b==1){ //need to negate result			a = 0 - a;			b = 0;		}	}	else{ //fall back for Windows or unknown		get_buffer_text(str);		check_text_response_match(str);		i = -1024; // <-the minimum parsable number		clear_buffer();		append_number(i);				while ((i < 1023) && (got_text_match() != 1)){ // <-the maximum parsable number			i = i + 1; 			clear_buffer();			append_number(i);			get_buffer_text(str);			check_text_response_match(str);		}		if(i == 1024) // <-the maximum parsable number + 1			b = 3;		else			a = i;	}	if(b==2) //error was detected		print_str("Error: Improperly formatted number");	else if(b==3)		print_str("Error: Number outside representable range");	else		print_big_str("number=",a,"");

 

I had any sense I'd have been posting these things to my own website as well, but the Blades Forge was so convenient that i got lazy. . .

Posted

They're there to be able to give an accurate error when the user enters a number outside of [-32768,32767]. The checks are a little messy because we're reading digit by digit, and 3280 and 32765 need to be accepted without error, while 32769 and 32800 need to generate an error.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...