2 registered members (Imhotep, opm),
785
guests, and 4
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Is Text Box data check possible?
[Re: gamers]
#479084
02/13/20 15:25
02/13/20 15:25
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Hey! If you store text into the string, you can check it's length ( str_len). As for 'numbers only' maybe str_stri can help, just a quick thought. Greets! Edit: I think str_stri was a bad idea, not going to work...probably str_cmpi is a better for this case. Edit2: I've spent a little time and came up with this (since I don't know how to handle regex with lite-c and it might not even support it):
#include <acknex.h>
#include <default.c>
#define PRAGMA_POINTER
STRING *non_numeric_str = "0123456789a";
STRING *numeric_str = "5325";
STRING *empty_str = "";
// receives a STRING and position (num) of a character in that STRING
// returns a single character as a STRING
STRING *get_next_char(STRING *src_str, int num)
{
if(!src_str)
{
return NULL;
}
if(num > str_len(src_str))
{
return NULL;
}
STRING *temp_str = "";
str_cpy(temp_str, src_str);
str_trunc(temp_str, str_len(src_str) - num);
str_clip(temp_str, num - 1);
return temp_str;
}
// returns TRUE (1) if string is numeric
// otherwise returs FALSE (0)
var is_numeric_str(STRING *src_str)
{
if(!src_str)
{
return false;
}
int i = 0, j = 0, res = true;
for (i = 0; i < str_len(src_str); i++)
{
STRING *temp_str = get_next_char(src_str, i + 1);
for (j = 0; j < 10; j++)
{
if (!str_cmpi(temp_str, str_for_num(NULL, j)))
{
res = false;
continue;
}
res = true;
break;
}
if(res == false)
{
break;
}
}
return res;
}
// returns TRUE (1) is string's length is zero
// otherwise returns FALSE (0)
var is_empty_str(STRING *str)
{
if(!str)
{
return false;
}
if(str_len(str) > 0)
{
return false;
}
else
{
return true;
}
}
// main game function
void main()
{
var numeric1 = is_numeric_str(non_numeric_str);
var numeric2 = is_numeric_str(numeric_str);
var empty = is_empty_str(empty_str);
while(!key_esc)
{
DEBUG_VAR(numeric1, 0);
DEBUG_VAR(numeric2, 20);
DEBUG_VAR(empty, 40);
wait(1);
}
} Edit3: my code above probably is not the best and easiest ways... I just wanted to give it a try. hopefully some other users may point out a better solution.
Last edited by 3run; 02/13/20 21:33.
|
|
|
Re: Is Text Box data check possible?
[Re: 3run]
#479087
02/14/20 12:50
02/14/20 12:50
|
Joined: Jan 2012
Posts: 108
gamers
OP
Member
|
OP
Member
Joined: Jan 2012
Posts: 108
|
Hey! If you store text into the string, you can check it's length ( str_len). As for 'numbers only' maybe str_stri can help, just a quick thought. Greets! Edit: I think str_stri was a bad idea, not going to work...probably str_cmpi is a better for this case. Edit2: I've spent a little time and came up with this (since I don't know how to handle regex with lite-c and it might not even support it):
#include <acknex.h>
#include <default.c>
#define PRAGMA_POINTER
STRING *non_numeric_str = "0123456789a";
STRING *numeric_str = "5325";
STRING *empty_str = "";
// receives a STRING and position (num) of a character in that STRING
// returns a single character as a STRING
STRING *get_next_char(STRING *src_str, int num)
{
if(!src_str)
{
return NULL;
}
if(num > str_len(src_str))
{
return NULL;
}
STRING *temp_str = "";
str_cpy(temp_str, src_str);
str_trunc(temp_str, str_len(src_str) - num);
str_clip(temp_str, num - 1);
return temp_str;
}
// returns TRUE (1) if string is numeric
// otherwise returs FALSE (0)
var is_numeric_str(STRING *src_str)
{
if(!src_str)
{
return false;
}
int i = 0, j = 0, res = true;
for (i = 0; i < str_len(src_str); i++)
{
STRING *temp_str = get_next_char(src_str, i + 1);
for (j = 0; j < 10; j++)
{
if (!str_cmpi(temp_str, str_for_num(NULL, j)))
{
res = false;
continue;
}
res = true;
break;
}
if(res == false)
{
break;
}
}
return res;
}
// returns TRUE (1) is string's length is zero
// otherwise returns FALSE (0)
var is_empty_str(STRING *str)
{
if(!str)
{
return false;
}
if(str_len(str) > 0)
{
return false;
}
else
{
return true;
}
}
// main game function
void main()
{
var numeric1 = is_numeric_str(non_numeric_str);
var numeric2 = is_numeric_str(numeric_str);
var empty = is_empty_str(empty_str);
while(!key_esc)
{
DEBUG_VAR(numeric1, 0);
DEBUG_VAR(numeric2, 20);
DEBUG_VAR(empty, 40);
wait(1);
}
} Edit3: my code above probably is not the best and easiest ways... I just wanted to give it a try. hopefully some other users may point out a better solution. Thanks for your contribution. I will try this as soon as possible.
|
|
|
Re: Is Text Box data check possible?
[Re: gamers]
#479095
02/17/20 14:05
02/17/20 14:05
|
Joined: May 2009
Posts: 5,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
I think it depends on how you created your text box. As far as I know, there is no default text box in acknex... if you use (f.e.) imgui there is already implemented text box with limitation etc.. You can check it out here.Best regards! Edit: maybe I misunderstood your question.. to input only 0...99 numbers, I would check for the length on the string (make sure it's numeric), if the length >= 3, then value is 100 and higher.
Last edited by 3run; 02/17/20 14:07.
|
|
|
Re: Is Text Box data check possible?
[Re: gamers]
#479100
02/18/20 07:20
02/18/20 07:20
|
Joined: Jul 2007
Posts: 619 Turkey, Izmir
Emre
User
|
User
Joined: Jul 2007
Posts: 619
Turkey, Izmir
|
So, you want to input box for only numbers, between 0&100. Here is another way. Not the good one but at least it can give some idea; #include <acknex.h>
STRING* defstr="#100";
STRING* test_str="";
//is input box open
int is_input_box_on=0;
//test variable
int test_num;
//debug
function textx_startup()
{
while(1)
{
if(is_input_box_on==1)
{
draw_text("Enter any number",1,1,COLOR_GREEN);
draw_text(test_str,1,200,COLOR_GREEN);
}
DEBUG_VAR(test_num,20);
wait(1);
}
}
function on_f1_event()
{
beep();
//clear all strings
str_cpy(test_str,"");
test_num=0;
char this_char = 0;
is_input_box_on=1;
while(this_char!=13)//wait until press enter
{
this_char = inchar(defstr);
if(this_char!=1)
{
//if is numeric
if(str_stri(this_char,"0")||
str_stri(this_char,"1")||
str_stri(this_char,"2")||
str_stri(this_char,"3")||
str_stri(this_char,"4")||
str_stri(this_char,"5")||
str_stri(this_char,"6")||
str_stri(this_char,"7")||
str_stri(this_char,"8")||
str_stri(this_char,"9"))
{
str_cat(test_str,defstr);
}
else
{
//if isn't numeric don't do anything
}
}
wait(1);
}
//convert string to number
test_num=str_to_int(test_str);
//turn of the box
is_input_box_on=0;
//if bigger than 100
if(test_num>100)
{
printf("Error: Value is bigger than 100!");
//try again
on_f1_event();
}
}
function main()
{
on_f1_event();
}
void on_esc_event()
{
sys_exit("");
}
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|