Hey what if you need a program to randomly choose several funtions from a list. Sort of like a hello program that responds in different ways.
I was wondering how to do this.
Hey what if you need a program to randomly choose several funtions from a list. Sort of like a hello program that responds in different ways.
I was wondering how to do this.
You could make a random number and use if statements or a switch statement to call the functions. Give us an example and maybe we can help.
Well, I'm making a chat bot thing (in cmd promt) using Dev C++.
This is what I have:
NOTE: Thats not all I have. I got the headers, declarations and other stuff too.Code:cout << "what is your name?" << endl; cin >> inputName if(inputName>= 20 char) cout << "Your REAL name, please." << endl; else cout << Thats a very funny name, << inputName << ". Very funny..." << endl;
So now I need to have the bot say different things. So I'll have 4 other possible sayings, and the bot will randomly choose to say them with the input.
Need help on that.
For what you are describing I think you'd probably be better off setting up an array of strings, then randomly select a response from the array. This way you could have an array of questions, array of statements, array of jokes etc.
If however you would like to randomly select functions, you can setup an array of pointers to functions then randomly select them. Here's an example....
Code:#include <iostream> #include <ctime> using namespace std; void function1(); void function2(); void function3(); int main() { int randFunc; void ( *func[3] )() = { function1, function2, function3 }; srand ( time(NULL) ); randFunc = rand() % 3; ( *func[randFunc] )(); cin.get(); return 0; } void function1() { cout << "Function 1 executed." << endl; } void function2() { cout << "Function 2 executed." << endl; } void function3() { cout << "Function 3 executed." << endl; }
as this looks like a very simple chat bot, you can score reponses (the scoring is upto your choosing), depending on the value of the score you display the proper response. This, however, is a highly inefficient way to do a chatbot. I suugest looking at http://www.alicebot.org/ for ideas and inspiration.
some entropy with that sink? entropysink.com
there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka
yeah, that is an idea, but if he wants to simulate some sort of intelligent response, random responses wont work.For what you are describing I think you'd probably be better off setting up an array of strings, then randomly select a response from the array. This way you could have an array of questions, array of statements, array of jokes etc.
If however you would like to randomly select functions, you can setup an array of pointers to functions then randomly select them. Here's an example....
But to follow Scribbler's thought, you could have an array of, lets say, "comforting" responses. You can check your user's input, and after parsing it you see that it contains, for example, the string "sad", then you randomly choose a response from your "comforting" array - you can do the same for different emotions - again, it might be good practice, but look into something more efficient and expandable.
some entropy with that sink? entropysink.com
there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka