Robot Help

Here is my code.

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
50

#include <iostream>
#include <string>


using namespace std;

string question;
string answer;

int main (int argc, const char * argv[])
{
    cout << "   _____ _ _ _              _   " << endl;
    cout << " / ____(_) | |             | |  " << endl;
    cout << "| |  __ _| | |__   ___ _ __| |_ " << endl;
    cout << "| | |_ | | | '_ \\ / _ \\ '__| __|" << endl;
    cout << "| |__| | | | |_) |  __/ |  | |_ " << endl;
    cout << " \\_____|_|_|_.__/ \\___|_|   \\__|" << endl;
//Ask Question
    char loop;
    do{
        string sentence;
        string search;
        size_t pos;
    
    cout << "Ask Gilbert a question:" << endl;
        getline(cin, sentence);
        
            
        search = "apple";
        pos = sentence.find(search);
        if (pos != string::npos)
            cout << "i likez thouse.";
        
        search = "lol";
        pos = sentence.find(search);
        if (pos != string::npos)
            cout << "i likez some.";

        
        //End Loop
        cout<< endl <<"Ask another?(Y/N)" << endl;
		cin>>loop;
		cout << string(3, '\n' );
    }while(loop=='y'||loop=='Y');
    if(loop=='n'){
        cout << "i didnt like you anyways...";
        return 0;
    }
}


Basically, you enter a sentence and Gilbert(my poor grammar robot) will answer it based on if it contains a word.

Here is the issue. Lets say I enter "I like apple lol."

This will print out

 
i likez thouse.i likez some.


How do I program Gilbert to only output one response?
Last edited on
set an initial count value to 0 add if count == 0 in your if statements and after it prints something increment count by one.
@giblit

I'm unsure about you mean. Please explain in code :)
Last edited on
1
2
3
4
5
6
7
int count = 0;
 search = "apple";
        pos = sentence.find(search);
        if (pos != string::npos && count == 0){
            cout << "i likez thouse.";'
            ++count;
       } 

ect....


Oh and btw this might be easier

if(sentence.find(search) && count == 0)

and then just get rid of that pos = thing its kinda useless imo
Last edited on
@giblit

Thanks, it works.

How can I make the "word to response" selection random? For example, if I said

"I like apple pear"

It wouldn't go through with the first, but a random one.

Isn't there a rand function?




EDIT: It seems as if the loop isnt working. I will say yes, and it will ask again.

If I ask the same question, it will end the program.
Last edited on
if you are inputing yes that is why. you are using cin. so you would have to ignore the other characters otherwise they will be in the buffer for the next cin. and as far as the random thing
you could do something like
1
2
3
4
5
6
7
8
9
string array[2] = { "pear", "apple"};
srand(time(NULL));
int random = rand() % 2 + 1;
search = array[random];
if(sentence.find(search[0]) && count == 0){
std::cout << "Answer one." << std::endl;
} else if(sentence.find(search[1] && count == 0){
std::cout << "Answer two." << std::endl;
}
@giblit

I'm unsure about you mean at the beginning. How would I do that?

Also, where would I put

1
2
3
4
5
6
7
8
9
10
11

string array[2] = { "pear", "apple"};
srand(time(NULL));
int random = rand() % 2 + 1;
search = array[random];
if(sentence.find(search[0]) && count == 0){
std::cout << "Answer one." << std::endl;
} else if(sentence.find(search[1] && count == 0){
std::cout << "Answer two." << std::endl;
}
http://www.cplusplus.com/reference/istream/istream/ignore/
something like
[code]
cin.ignore(1000,'\n');
and you would want to put the string outside of the do loop but inside the main function. and the srand and random inside the do loop towards the top and then the rest of it you can you inplace of the if/else stuff you have currently
Topic archived. No new replies allowed.