Understated Ur-Drakon Celtic Minstrel Posted July 31, 2011 Posted July 31, 2011 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. Quote
Well-Actually War Trall Niemand Posted July 31, 2011 Posted July 31, 2011 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. . . Quote
Understated Ur-Drakon Celtic Minstrel Posted July 31, 2011 Author Posted July 31, 2011 What's with the 3276 checks? Quote
Well-Actually War Trall Niemand Posted July 31, 2011 Posted July 31, 2011 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. Quote
Understated Ur-Drakon Celtic Minstrel Posted August 1, 2011 Author Posted August 1, 2011 Oh, I think I get it. It's a pity there's only a Mac version for the parsing, though. Oh well. Quote
Well-Actually War Trall Niemand Posted August 1, 2011 Posted August 1, 2011 Sadly, a Windows version appears to be impossible because of the limitations of our 22 bit addressing system. Quote
Well-Actually War Trall Ishad Nha Posted August 2, 2011 Posted August 2, 2011 There is always my one-digit-at-a-time approach. All that is needed here is "cin", Ascript does not even have that! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.