【发布时间】:2019-07-21 17:46:31
【问题描述】:
无法从第二个数组中获取索引以匹配第一个数组
// array constants
const int people = 7;
const int phoneNumbers = 7;
int main() {
char person, family;
int index;
string found;
int size =7;
//array declaration and set values
string people [] ={"Darryl","Jasmine","Brian","Duane","Ayana","Mia","Maya"};
// const char phoneNumbers = 7;
string phoneNumbers[] = {"678-281-7649", "818-933-1158", "212-898-2022",
"361-345-3782","817-399-3750","313-589-0460","818-634-4660"};
//set boolean value
found = "False";
//initialize index
index = 0;
// search variable and user input
cout << "who are you looking for? " << endl;
cin >> people[index];
for (index=0; index<=6; index--) {
if (people[index] == people[index] )
cout << "phone num for " << people[index] << " is "<<
phoneNumbers[index] << endl;
}
return 0;
}
当我放入 people[] 数组的 Jasmine 时,phoneNumbers[] 数组带回 phoneNumbers[] 的第一个索引,它应该带回 phoneNumbers[] 数组上的第二个索引
【问题讨论】:
-
您对
people[index] == people[index]有何期待?永远正确。 -
验证来自用户的输入是否与 people[index] 内容匹配。如何确保用户的输入与 people[index] 匹配,然后循环电话号码以匹配
-
//initialize index index = 0;- 为什么在声明index时不这样做?? -
cin >> people[index];您的用户输入会覆盖您的参考数据... -
我是学生,还在学习.. 你的意思是像这样初始化索引 - index = people[index]