因此,我目前正在研究一个项目,该项目需要我从用户那里获取数组的单词,然后让他们搜索数组中的单词。程序必须显示数组中单词的位置。以下是我所做的:如何搜索字符二维数组中的单词?
#include <iostream.h>
#include <conio.h>
main()
{
char studentsList[30][20];
int size, flag = 0, pos;
cout << "Enter the size of the array: ";
cin >> size;
cout << "Enter yhe names of the students: \n";
for(int i = 0; i < size; i++)
{
cout << "Student no. " << i + 1 << ": ";
cin >> studentsList[i];
}
for(int m = 0; m < size; m++)
{
cout << studentsList[m] << "\t";
}
char searchName[20];
cout << "type the name you need to search: ";
cin >> searchName;
for(i = 0; i < size; i++)
{
if(studentsList[i] == searchName)
{
flag = 1;
pos = i + 1;
}
}
if(flag == 0)
cout << "Term not found.";
else
cout << "Term found at position " << pos;
getch();
}
我无法捕捉代码中的错误。它总是将输出作为'未找到期限'。帮助将不胜感激!
这不是C++。由于几个原因,它不会编译。在修复代码以便编译代码时,可以借助调试器轻松解决问题。 – mpiatek
请参阅[本文](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c)了解如何比较C字符串。请注意,如果你使用'std :: string',你的生活会更容易。 – Rakete1111
您使用的头文件都不是标准C++的一部分。无论你从哪里学习这些东西,都要从其他地方学习。 –