.find help!

I am writing a stupid robot named Gilbert. You would enter a question, he would pick out a word, and reply to it.

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
#include <iostream>
#include <string>

using namespace std;

string question;

int main (int argc, const char * argv[])
{
//Output Picture,
    cout << "   _____ _ _ _              _   " << endl;
    cout << " / ____(_) | |             | |  " << endl;
    cout << "| |  __ _| | |__   ___ _ __| |_ " << endl;
    cout << "| | |_ | | | '_ \\ / _ \ '__| __|" << endl;
    cout << "| |__| | | | |_) |  __/ |  | |_ " << endl;
    cout << " \\_____|_|_|_.__/ \\___|_|   \\__|" << endl;
//Ask Question
    cout << "Ask me a question!";
    cin >> question;
    if (string::npos != question.find("apple"))
    {
        cout << "found!" << endl;
    }
    
    else if (string::npos != question.find("sauce"))
    {
        cout << "found!" << endl;
    }
    
    else{
        cout << "Not found!";
    }

}



Lets say you enter JUST sauce. It will say found. If not, not found.

The issue is I want the user to be able to enter "I like sauce" and for Gilbert to say found. Currently, if you do anything more than sauce, he says "Not found!"

How do I solve this? Thanks
You need to split the input from the user into a vector or array, iterate over this and check for the existence of every word is has in it.

You will want to not use cin >> and instead use getline()
http://www.cplusplus.com/forum/articles/6046/
@Zaita
I am new to C++, how would I split the input from the user into a vector or array, iterate over this and check for the existence of every word is has in it?
It's not exactly pretty, but this is some native code I wrote many years ago to do it. These days I'd just use Boost and do it in 2 lines:

 
// Oops wrong code 
Last edited on
I don't understand :O

Last edited on
Err ignore that code. This code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void CConvertor::stringToVectorByNewline(string value, vector<string> &lines) {
  // Variables
  string  sLine         = value;
  int     iFirstSpace   = -1;

  iFirstSpace = sLine.find_first_of(' ');
  if (iFirstSpace == -1) {
    lines.push_back( sLine );
    return;
  }

  while (iFirstSpace >= 0) {
    // Check Difference Between Our Spaces
    if (iFirstSpace > 0)
      lines.push_back( sLine.substr(0, iFirstSpace));

    sLine = sLine.erase(0, iFirstSpace+1);
    iFirstSpace = sLine.find_first_of(' ', 0);
  }
  // If anything is remaining, add it to the list
  if (sLine.length() > 0)
    lines.push_back(sLine);
}


If I gave it "There was a duck named bill" I'd get back
1
2
3
4
5
6
7
vector<string> values;
string input "There was a duck named bill"

doConvert(input, values);

values.size() == 6;
values[0] == "There"; // etc 
And the boost equal to that is:
1
2
3
4
string input = "There was a duck named bill";
vector<string> parts;

boost::split(parts, input, boost::is_any_of(" "));
@Zaita

This is confusing... I don't understand.

How would I implement this into my program, and how would I "create" a question.
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
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <vector>

using namespace std;

void splitString(string value, vector<string> &lines) {
  // Variables
  string  sLine         = value;
  int     iFirstSpace   = -1;

  iFirstSpace = sLine.find_first_of(' ');
  if (iFirstSpace == -1) {
    lines.push_back( sLine );
    return;
  }

  while (iFirstSpace >= 0) {
    // Check Difference Between Our Spaces
    if (iFirstSpace > 0)
      lines.push_back( sLine.substr(0, iFirstSpace));

    sLine = sLine.erase(0, iFirstSpace+1);
    iFirstSpace = sLine.find_first_of(' ', 0);
  }
  // If anything is remaining, add it to the list
  if (sLine.length() > 0)
    lines.push_back(sLine);
}

int main() {

	string input = "";
	while (true) {
		cout << "Please enter a sentence (or q to quit):";
		getline(cin, input);
		if (input == "q" || input == "quit")
			break;
		cout << "You entered: " << input;

		vector<string> pieces;
		splitString(input, pieces);
		cout << "You sentence has " << pieces.size() << " words" << endl;
		for (unsigned i = 0; i < pieces.size(); ++i)
			cout << "Piece " << i + 1 << " = " << pieces[i] << endl;
	}

	return 0;
}
@Zaita

I'm sorry for being confused. I understand it a bit more, but how would I do something like I was before?

1
2
3
4
if (string::npos != question.find("apple"))
    {
        cout << "found!" << endl;
    }
Look at my code line 44. That shows you how to get each piece. Then you just need to check each piece for the value you want and keep track of if it has been found or not.
@Zaita

I think I understand now.

One last question. How would I write an if statement to see if the whole sentence contains something?
It's not so much of an if statement than a loop that maintains a variable to see if the object is found.
bool isFound = false; Then do your loop and when you find it change isFound to true. After the loop if isFound is true print yes, if not print no etc.
@Zaita

I understand the boolean part... but I'm unsure about what you mean by "my loop"

Last edited on
Topic archived. No new replies allowed.