如果条件满足,则代码无法正确执行代码
此代码从对话框中获取一个字符串,并将其与列表中的数据进行比较,如果成功,则设置对列表中元素的选择。 我遇到的问题是 if ,如果我只搜索第一个元素,它会起作用,如果我尝试搜索任何其他元素,它只会忽略 if 条件并一直到列表末尾。
void CMFC1Dlg::OnBnClickedButton6()
{
CString variable;
cautare.GetWindowTextA(variable);
variable = variable.MakeLower();
if(variable!="")
{
list<Contact*>::iterator seek;
bool flag = TRUE;
int i = 0 ;
while(flag)
{
seek = agenda.first_element();
if( ((CString)((*seek)->getLastName().c_str())).MakeLower() == variable ||
((CString)((*seek)->getFirstName().c_str())).MakeLower() == variable ||
((CString)((*seek)->getFirstAndLastName().c_str())).MakeLower() == variable ||
((CString)((*seek)->getLastAndFirstName().c_str())).MakeLower() == variable)
{
contactsVariable.SetCurSel(i);
this->OnLbnSelchangeList1();
flag=FALSE;
}
advance(seek,i);
i++;
if (i == agenda.list_size())
{
flag = FALSE;
}
}
}
else
MessageBox("No text on input ", "Error", MB_ICONERROR | MB_OK);
cautare.SetFocus();
cautare.SetWindowTextA("");
}
This code takes a string from a dialog box , and compares it to data in a list,and if succesfull sets the selection on the element from list .
The problem i am having is with the if , it works if i only search the first element , if i try to search any other , it just ignore the if condition and goes till the end o the list .
void CMFC1Dlg::OnBnClickedButton6()
{
CString variable;
cautare.GetWindowTextA(variable);
variable = variable.MakeLower();
if(variable!="")
{
list<Contact*>::iterator seek;
bool flag = TRUE;
int i = 0 ;
while(flag)
{
seek = agenda.first_element();
if( ((CString)((*seek)->getLastName().c_str())).MakeLower() == variable ||
((CString)((*seek)->getFirstName().c_str())).MakeLower() == variable ||
((CString)((*seek)->getFirstAndLastName().c_str())).MakeLower() == variable ||
((CString)((*seek)->getLastAndFirstName().c_str())).MakeLower() == variable)
{
contactsVariable.SetCurSel(i);
this->OnLbnSelchangeList1();
flag=FALSE;
}
advance(seek,i);
i++;
if (i == agenda.list_size())
{
flag = FALSE;
}
}
}
else
MessageBox("No text on input ", "Error", MB_ICONERROR | MB_OK);
cautare.SetFocus();
cautare.SetWindowTextA("");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
既然您正在迭代元素列表,为什么不使用关联列表的
begin()
和end()
方法。因此,您可以迭代每个元素,而无需在每个循环中推进迭代器。代码可能如下所示
Since you are iterating over a list of elements, why not use the
begin()
andend()
method of the associated list. So you can iterate over each element and don't need to advance the iterator in each loop.The code could look like this
您只比较第一个元素 move
advance(seek,i);
,如下所示:You are only ever comparing the first element move
advance(seek,i);
like this:您在 while 循环的每次迭代开始时设置
seek =agenda.first_element();
。将该语句移到循环之外,它应该可以工作。编辑:您还需要将搜索调用更改为仅搜索 1,而不是 i,因为您不再丢弃先前搜索的结果。
You are setting
seek = agenda.first_element();
at the beginning of every iteration of the while loop. Move that statement outside the loop and it should work.EDIT: You would also need to change the seek call to only seek 1, rather than i, since you're no longer throwing out the result of the previous seeks.
您应该将
seek =agenda.first_element();
移出 while 循环。You should move
seek = agenda.first_element();
out of the while loop.