Help in my RPG

I am trying to make a text RPG and need help so that when the user types in a certain command it will do a certain thing but I have a set amount of commands that they can type in. Basically I made a loop to go through a vector of strings and if the input is equal to one of the strings it stops and says something but if it isnt equal to the string is says something else. Only problem is it will go through and each time it is not equal to the string it says the wrong on all the instances.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
    int HP;
    int maxHP;
    int MP;
    int maxMP;
    char name[12];
    int invenSpace;
    vector <string> keyWords;
    keyWords.reserve(5);
    keyWords.push_back("USE");
    keyWords.push_back("GO");
    keyWords.push_back("ATTACK");
    keyWords.push_back("RUN");
    keyWords.push_back("GRAB");


    cout << "BASIC COMMANDS" << endl;
    cout << "USE, GO, ATTACK, RUN, GRAB" << endl;
    cout << "" << endl;
    cout << "You are walking into a forest and see a bear amongst" << endl;
    cout << "the trees. He goes to attack you." << endl;
    cout << "What do you do???" << endl;
    char input[100];
    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)){
            cout << "Ok now what?" << endl;
        }else{
            cout << "Incorrect input..." << endl;
        }
    }
    
}
each time it is not equal to the string it says the wrong on all the instances.

That's because line 38 is inside your loop.

You want something like this:
1
2
3
4
5
6
7
8
9
10
  bool found = false;
  for (int i=0; i<keyWords.size(); i++)
  {  if (input == keyWords.at(i))
     {  cout << "Ok now what?" << endl;
         found = true;
         break;  // We can exit the loop
    }
    if (! found) 
      cout << "Incorrect input..." << endl;      
  }


It would make a lot of sense to put this in a function. Have the function return i when you find a match. Don't return out of the function until you have a valid keyword.


Thank you :)
Could you explain how I would set this up in a function?
Like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int userInput() {
    
    vector <string> keyWords;
    keyWords.reserve(5);
    keyWords.push_back("USE");
    keyWords.push_back("GO");
    keyWords.push_back("ATTACK");
    keyWords.push_back("RUN");
    keyWords.push_back("GRAB");
    
    char input[100];
    cin >> input;
    for (int i = 0; input[i] != '\0'; i++){
        input[i]=toupper(input[i]);
    }
    bool found = false;
    for (int i=0; i<keyWords.size(); i++){  
        if (input == keyWords.at(i)){
            cout << "Ok now what?" << endl;
            found = true;
            break;  // We can exit the loop
        }
    }
        
    if (! found) {
        cout << "Incorrect input..." << endl;      
    }
}


Sort of.

You don't have a return statement. The reason for putting this in a function was to return the index of the object.

Replace lines 20-21 with a return statement.
20
21
 
  return i;


The other reason for putting this in a function was to incorporate a loop so that you continue to prompt the user until they enter an acceptable answer. What happens with your code, if a match is not found? You display "Incorrect input", but then return to the caller.

12
13
14
15
16
17
18
19
20
21
22
23
24
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))
      {  cout << "Ok now what?" << endl;
          return i; 
       }
    }      
    cout << "Incorrect input..." << endl;      
}

Topic archived. No new replies allowed.