C++ Ncurses表单中的字段验证有问题
我对ncurses表单中的字段验证有一些问题,希望有人能对此做出解释。 该问题与下面所附的代码有关。特别是在函数C++ Ncurses表单中的字段验证有问题,c++,ncurses,curses,C++,Ncurses,Curses,我对ncurses表单中的字段验证有一些问题,希望有人能对此做出解释。 该问题与下面所附的代码有关。特别是在函数validateField()中名为“AuthorityCode”的字段中。 ENUMAuth_codes设置为它的三个值,当输入正确的值(其中一个ENUM值)时,它工作正常。它还验证大多数不正确的值(不在枚举中的值)。但是,出于某种原因,它接受字段中的单个数字“1”、“2”、“3”、“4”、“5”、“6”或“8”,而不是“7”或“9”。似乎没有任何一个字母被接受,也没有多个数字的组合
validateField()
中名为“AuthorityCode”的字段中。
ENUMAuth_codes
设置为它的三个值,当输入正确的值(其中一个ENUM值)时,它工作正常。它还验证大多数不正确的值(不在枚举中的值)。但是,出于某种原因,它接受字段中的单个数字“1”、“2”、“3”、“4”、“5”、“6”或“8”,而不是“7”或“9”。似乎没有任何一个字母被接受,也没有多个数字的组合,只有上面的数字。
所有其他验证(包括其他枚举)似乎都在正常工作
是否有人对这种行为有解释,或者可以在我的代码中找到问题
const char* Auth_codes[]
{ "Add", "Update", "Delete" };
const char* Return_codes[]
{ "Accepted", "Denied" };
const char* Priorities[]
{ "3", "5", "6", "8" };
void createForm(WINDOW *win_body, vector<string> &input, int selection)
{
int rows;
int cols;
int i;
int cury = 0;
int curx = 1;
int nb_fields;
WINDOW *inner;
formClose = false;
getmaxyx(win_body, rows, cols);
nb_fields = input.size()
fields = (FIELD**) malloc(sizeof(FIELD *) * (nb_fields + 1));
assert(fields);
for (i = 0; i < nb_fields; i++)
{
fields[i] = new_field(1, 10, cury, curx, 0, 0);
assert(fields[i] != NULL);
set_field_buffer(fields[i], 0, "");
field_opts_on(fields[i], O_ACTIVE);
field_opts_on(fields[i], O_EDIT);
field_opts_off(fields[i], O_AUTOSKIP);
set_field_back(fields[i], A_UNDERLINE);
validateField(fields[i], input[i].c_str()); // Set correct field validation based on label value
curx = 1; // Set position for next label field
cury = cury + 1; // Next line
}
fields[i] = NULL;
form = new_form(fields);
assert(form != NULL);
win_form = derwin(win_body, rows - 10, cols - 4, 3, 2);
box(win_form, 0, 0);
assert(form != NULL && win_form != NULL);
set_form_win(form, win_form);
inner = derwin(win_form, form->rows + 1, form->cols + 1, 1, 1);
assert(inner != NULL);
set_form_sub(form, inner);
assert(post_form(form) == E_OK);
assert(post_menu(menu) == E_OK);
refresh();
wrefresh(win_body);
wrefresh(win_form);
while (formClose == false)
process_input(getch());
delete_form();
}
void validateField(FIELD *field, const char *name)
{
if (strcmp(name, "AdviceNoteNumber") == 0)
{
set_field_type(field, // Field to alter
TYPE_ALNUM, // Type to associate
0); // Minimum field width
}
else if (strcmp(name, "AssignmentId") == 0 || strcmp(name, "AssignmentReferenceId") == 0
|| strcmp(name, "TransportAssignmentId") == 0)
{
set_field_type(field, // Field to alter
TYPE_INTEGER, // Type to associate
0, // Number of padding zeroes
10000, // Min value
99999); // Max value
}
else if (strcmp(name, "AuthorityCode") == 0)
{
set_field_type(field, // Field to alter
TYPE_ENUM, // Type to associate
Auth_codes, // List of values
0, // Not case sensitive
1); // Unique prefix
}
else if (strcmp(name, "ReturnCode") == 0)
{
set_field_type(field, // Field to alter
TYPE_ENUM, // Type to associate
Return_codes, // List of values
0, // Not case sensitive
1); // Unique prefix
}
else if (strcmp(name, "Priority") == 0)
{
set_field_type(field, // Field to alter
TYPE_ENUM, // Type to associate
Priorities, // List of values
0, // Not case sensitive
1); // Unique prefix
}
const char*Auth_code[]
{“添加”、“更新”、“删除”};
常量字符*返回_代码[]
{“接受”、“拒绝”};
常量字符*优先级[]
{ "3", "5", "6", "8" };
void createForm(窗口*win_体、向量和输入、整数选择)
{
int行;
int cols;
int i;
int cury=0;
int-curx=1;
int nb_字段;
窗口*内部;
formClose=false;
getmaxyx(win_body、rows、cols);
nb_fields=input.size()
字段=(字段**)malloc(sizeof(字段*)*(nb_字段+1));
断言(字段);
对于(i=0;i行+1,表单->列+1,1,1);
断言(内部!=NULL);
集合形式(形式,内部);
断言(post_form(form)=E_OK);
断言(post_菜单(菜单)==E_OK);
刷新();
wrefresh(win_body);
wrefresh(win_form);
while(formClose==false)
处理输入(getch());
删除表格();
}
void validateField(字段*字段,常量字符*名称)
{
如果(strcmp(名称,“AdviceNoteNumber”)==0)
{
设置字段类型(字段,//要更改的字段
键入\ u ALNUM,//要关联的类型
0);//最小字段宽度
}
else if(strcmp(名称,“AssignmentId”)==0 | | strcmp(名称,“AssignmentReferenceId”)==0
||strcmp(名称,“TransportAssignmentId”)==0
{
设置字段类型(字段,//要更改的字段
键入\ u整数,//要关联的类型
0,//填充零的数目
10000,//最小值
99999);//最大值
}
else if(strcmp(名称,“AuthorityCode”)==0)
{
设置字段类型(字段,//要更改的字段
TYPE_ENUM,//要关联的类型
验证码,//值列表
0,//不区分大小写
1) ;//唯一前缀
}
else if(strcmp(名称,“返回代码”)==0)
{
设置字段类型(字段,//要更改的字段
TYPE_ENUM,//要关联的类型
返回\u代码,//值列表
0,//不区分大小写
1) ;//唯一前缀
}
else if(strcmp(名称,“优先级”)==0)
{
设置字段类型(字段,//要更改的字段
TYPE_ENUM,//要关联的类型
优先级,//值列表
0,//不区分大小写
1) ;//唯一前缀
}
set_field_type
的第三个参数是可接受的枚举值列表,指定为char**
这不能是一些未指定大小的指针列表。显然,可接受枚举值的数量,这里的指针数量,必须以某种形式或方式定义。在set\u field\u type
s文档中没有明确指出如何定义可接受枚举值的数量,这只是C+**/CONT>参数。但是在C和C++中,假定一个指针列表被一个<代码> null <代码>指针所终止,这是一个公认的实践;教科书的典型定义是“<代码> null <代码>指针”的一部分在这些边界内下降。
const char* Priorities[]=
{ "3", "5", "6", "8" };
该参数是一个由四个char*
s组成的列表,按原样传递。当它进入set\u field\u type
时,它只是一些char**
值。这里没有明确定义它为四个指针,因此set\u field\u type
试图找出有多少个枚举值,从这里开始查找,对四个指针计数,然后继续计数,继续计数,进入随机内存,尝试将每个值解释为一个char*
,直到它在某处找到它认为是空的NULL
值,同时尝试将每个中间值解释为char*
,指向去某个地方
如果现在还没有崩溃,它将开始看到虚有效枚举值,从而导致错误
设置字段类型的第三个参数是l
const char* Priorities[]=
{ "3", "5", "6", "8" };