<分区>
我是 C++ 初学者,所以跟我说话就像我才 5 岁一样。
这是我正在尝试做的事情:
将用户的输入转化为字符串
userInput
将
userInput
连同 2 个数组(answers
和outcomes
)传递到函数answerCheck
比较
userInput
与answers
数组如果匹配,从
输出字符串outcomes
如果没有匹配,循环,请求
userInput
我使用 answersSize
输出 answers
的大小。它输出 1 而不是预期的 2。
我不知道如何将数组中的信息传递给 answerCheck
函数。
有什么建议吗?
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int question1();
bool answerCheck(string[], string[], string);
int main() {
question1();
system("pause");
return 0;
}
int question1() {
cout << "Do you want to go LEFT or RIGHT?" << endl;
string answers[2] = { "left", "right" };
string outcomes[2] = { "you went left", "you went right" };
string userInput = "";
getline(cin, userInput);
// outputs correct size of answers array for testing ======
int answersSize = sizeof(answers) / sizeof(string);
cout << "Correct size of answers: "<< answersSize << endl;
// ========================================================
answerCheck(answers, outcomes, userInput);
return 0;
}
bool answerCheck(string answers[], string outcomes[], string userInput){
int answersSize = sizeof(answers) / sizeof(string);
cout << "Size of answers: "<< answersSize << endl;
for(int i=0; i < answersSize; i++){
if(userInput.find(answers[i]) != string::npos){
cout <<"\n" << outcomes[i] <<"\n" << endl;
return true;
}
}
cout << "Try putting in something else." << endl;
return false;
}