Apr 25, 2013 at 12:38am UTC
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 Apr 25, 2013 at 12:43am UTC
Apr 25, 2013 at 12:44am UTC
set an initial count value to 0 add if count == 0 in your if statements and after it prints something increment count by one.
Apr 25, 2013 at 12:45am UTC
@giblit
I'm unsure about you mean. Please explain in code :)
Last edited on Apr 25, 2013 at 12:46am UTC
Apr 25, 2013 at 12:50am UTC
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 Apr 25, 2013 at 12:51am UTC
Apr 25, 2013 at 12:54am UTC
@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 Apr 25, 2013 at 1:05am UTC