【发布时间】:2015-12-02 23:28:46
【问题描述】:
我有一个包含两个名称的结构,但它们是字符形式 - 所以是一个数组 字符[2][10] 它应该是两个最多十个字符长的名称。 我想搜索它们。
while (ans3==1)
{
cout << "\nPlease enter the name you want to search"
<<endl;
cin >> searchName;
for (int i=0; i < size; i++)
{
cout <<"\nsearching " <<endl;
for (int k=0; k< 10; k++ )
if ( MyData.name[i][k]==searchName[k])
{
cout << "\nName was found at position "<< k <<endl;
}
else
cout << "\nName not found at position " <<k <<endl;
}
cout << "\nDo you want to search for a name? (1 for y, 2 for n)" <<endl;
cin >> ans3;
}
这编译,但不做我想让它做的事。有人可以帮忙吗。谢谢。
【问题讨论】:
-
for (int k=0; k< 10; k++ )如果字符串少于 9 个字符,将在字符串末尾之后继续搜索。考虑改用strncmp。
标签: c++ string file search struct