C++で文字列の配列を検索できるようにしたい。私はこのデータを持っています:
"Foo Becky, 924-334-2514",
"Becky Warren, 555-1223",
"Geri Palmer, 555-8787",
"Ron Palmer, 555-2783"
ユーザーがと入力Bec
すると、プログラムは名前を見つけますFoo Becky, 924-234-2314
。ユーザーがと入力するとPalmer
、プログラムは次のように表示Geri Palmer, 555-8787
されます。Ron Palmer, 555-2783
これが私がこれまでに持っているものです:
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
string search;
while(1){
cout << "How many data you want to input: "<< endl;
cin >> n;
cin.ignore(1000, 10);
if(n > 0 && n < 20){
break;
}
cout << "Number of data can not be negative or more than 20. "<< endl;
}
string* data = new string[n];
for(int i=0; i< n; i++){
cout << "Enter [First Name] [Last Name], [Phone-Number] and then hit "
<< "enter." << endl << "e.g: Foo Becky, 925-245-413"<< endl;
getline(cin,data[i]);
cout << endl;
}
cout << "Enter what you want to search for: "<< endl;
getline(cin, search);
for(int i =0; i< n; i++){
if(search == data[i]){
cout << data[i]<< endl;
}
}
delete [] data;
return 0;
}
C ++で文字列の配列を検索するにはどうすればよいですか?