以前玩过C++,那是大一,大二的时候的事情了,一转眼,现在是大四了。由于想找工作,不得不重新学一下VC,于是拿了孙皓老师的Vc++范例大全,学到了CString这节,然后运行实例,发现了一个小小的临界值错误。

 

void CSearchReplaceDlg::OnSearch()
{
 // TODO: 在此添加控件通知处理程序代
 UpdateData(true);
 if(m_strFind.IsEmpty()){
  AfxMessageBox(L"查找的字符串为空");
  return;
 }
 int pos=0;
 CString strpos,temp;
 strpos.Format(L"%s字符串在原字符串中的起始位置为:\n",m_strFind);
 while((pos>=0)&&(pos<m_strData.GetLength())){
  pos=m_strData.Find(m_strFind,pos);
  if(pos>0){
   temp.Format(L"%d,",pos);
   strpos+=temp;
   pos+=m_strFind.GetLength();
  }
 }
 AfxMessageBox(strpos);
}

 不管怎么运行,都还有点小错误。你能知道错误在哪吗?

下面我贴出改过的代码,如果,你不仔细看,会发现这两次代码是一样的。

void CSearchReplaceDlg::OnSearch()
{
	// TODO: 在此添加控件通知处理程序代
	UpdateData(true);
	if(m_strFind.IsEmpty()){
		AfxMessageBox(L"查找的字符串为空");
		return;
	}
	int pos=0;
	CString strpos,temp;
	strpos.Format(L"%s字符串在原字符串中的起始位置为:\n",m_strFind);
	while((pos>=0)&&(pos<m_strData.GetLength())){
		pos=m_strData.Find(m_strFind,pos);
		if(pos>=0){
			temp.Format(L"%d,",pos);
			strpos+=temp;
			pos+=m_strFind.GetLength();
		}
	}
	AfxMessageBox(strpos);
}

 23:12:07

posted on 2013-11-25 23:12  华四  阅读(196)  评论(0编辑  收藏  举报