私は現在、配列の単語をユーザーから取り出し、配列内の単語を検索するように要求するプロジェクトに取り組んでいます。プログラムは、配列内の単語の位置を示す必要があります。ここで私は何をしたのですか:char 2D配列の単語を検索する方法は?
#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();
}
私はコードの何が間違っているかを捕まえることができません。常に 'Term not found'と出力されます。ヘルプは高く評価されます!
これは、C++ではありません。それはいくつかの理由でコンパイルされません。あなたの問題は、コードを修正してコンパイルできるようになると、デバッガの助けを借りて簡単に解決できます。 – mpiatek
Cの文字列を比較する方法については、[このポスト](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c)を参照してください。 'std :: string'を使った方があなたの人生が楽になることに注意してください。 – Rakete1111
使用するヘッダーファイルのいずれもStandard C++の一部ではありません。どこからでもこのことを学び、どこからでも学ぶことができます。 –