It's best to keep to one thread. It avoids confusion.
I don't really understand what return i does though |
The purpose of the
return i;
is to communicate back to the caller which keyword was selected.
This brings up a style issue. Do want userInput() to act on what was typed (as in RUN), or do you want userInput() to simply get a keyword and return the index to that keyword back to the caller?
Also how would I pass the vector into userInput |
1 2 3 4 5 6 7 8 9 10
|
// line 7
int userInput (const vector<string> & keywords);
// line 35
userInput (keywords);
// line 40
int userInput (const vector<string> & keywords)
// Delete lines 41-48
| |
Although with your current code it's not necessary to pass it. lines 17-23 are extraneous (not used).
lines 63, 89, 105, 107: The whole point of returning a value back to the caller, is to let the caller know the outcome of the function. You're passing back either i or 0. Since you currently have only one enemy, i can only be 0 since "BEAR" is the 0th entry.
lines 94-107: You need a loop here also (like in userInput) so that you repeat the question if the user did not type a valid answer.
edit:
Note that you're doing exactly the same thing in userInput and run, that is you're searching a vector of strings. Anytime you do something more than once, it suggests you need a function.
Consider calling the following from both userInput and run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// Search the passed vector for a keyword
// Returns the index of the keyword that was found,
// Loops until a valid keyword is entered.
int find_keyword (const vector<string> & keywords)
{ char input[100];
while (true)
{ cin >> input;
for (int i = 0; input[i] != '\0'; i++)
{ input[i]=toupper(input[i]);
}
for (int i=0; i<keywords.size(); i++)
{ if (input == keywords.at(i))
return i; // Return the index of the entry
}
// Not found
cout << "'" << input << "'" << " not a valid keyword." << endl;
}
}
| |
run() becomes simpler:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int run()
{ int enemy;
vector <string> enemies;
enemies.reserve(5);
enemies.push_back("BEAR");
cout << "Run where?" << endl;
enemy = find_keyword (enemies);
switch (enemy)
{
case 0:
cout << "You can't escape from the bear." << endl;
}
return 0;
}
| |