Find a string in a string

Nov 6, 2015 at 3:55am
Write your question here.
Ok so basically all I want to be able to do is have the user input something,and then check through whatever they typed to see if they typed a certain word and then if they did type that certain word, do something.
1
2
3
4
5
6
7
8
9
10
11
12
    vector <string> directions;
    directions.push_back("NORTH");
    directions.push_back("EAST");
    directions.push_back("SOUTH");
    directions.push_back("WEST");

    string input;
    cin >> input;
    if (//input has North){
        //do this
    }
    
Nov 6, 2015 at 4:07am
Nov 6, 2015 at 9:37am
thank you was looking for this too
Nov 6, 2015 at 10:35am
Hope this is what you are looking for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
using namespace std;

int main() {
	vector<string> directions;
	directions.push_back("NORTH");
	directions.push_back("EAST");
	directions.push_back("SOUTH");
	directions.push_back("WEST");

	string input;
	cin >> input;
	int flag = 1; // flag is set as false
	for (vector<string>::iterator it = directions.begin(); it < directions.end(); ++it) {
		if (input == *it) {
			flag = 0;
		}
	}
	if(flag == 0) cout << "success";
	else cout << "fail";
	return 0;
}
Nov 6, 2015 at 5:42pm
Mehakb that is almost what I am looking for, it does help but the problem is it only works if the person just types in north, south, east, west, they cant type "go north" or anything like that
Nov 6, 2015 at 5:43pm
Oh and could you also explain what the iterator does? Ive never used that command
Nov 19, 2015 at 6:22am
Is it compulsory to use vector? If not then you can do it with string, as string has
str.find();
Topic archived. No new replies allowed.