Help with making an automated response genreator

Basically I have made a code that generates only one response, I have collected around 100 responses from my friend, and the whole idea is to get a different response every time, doesn't matter what question is asked.

I would greatly appreciate any help to make this, because my friends responses are just hillarious


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
  #include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>

int main () {
    srand(time(0));
    std::string mystr;
    std::string possibleanswers[2] = {"good", "bad"};

    std::cout <<"What is your name?"; //name
    getline (std::cin, mystr);
    std::cout <<"Hello " << mystr << ", nice to meet you.";
    std::cout <<" What is your question today?"; //how are you

    getline (std::cin, mystr);
    if (mystr == "why today suck")
        std::cout <<"Theb you stupid";
    else if (mystr == "bad")
        std::cout <<"That isn't good.";
    else
        std::cout <<"You stupid fuck";
        

    getline (std::cin, mystr);
    if (mystr == "you?")
        std::cout <<"I am " << possibleanswers[rand() %2];
}


Here is the link to all the responses I have gathered, just in case https://www.dropbox.com/s/1psdthtl53jvukj/UldisResponses.txt?dl=0
Last edited on
If you need me to explain what I did, feel free to ask.

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()
{
  srand(time(NULL));
    std::string mystr;
    std::string possibleanswers[] = {"good", "bad", "horrible", "fantastic"};
    std::string word;

    std::cout <<"What is your name?"; //name
    getline (std::cin, mystr);
    std::cout <<"Hello " << mystr << ", nice to meet you.";
    std::cout <<" What is your question today?"; //how are you
    getline (std::cin, mystr);

    if (mystr == "why today suck")
        std::cout <<"Theb you stupid ";
    else if (mystr == "bad")
        std::cout <<"That isn't good. ";
    else
        std::cout <<"You stupid fuck ";

     for(int i=0; i<1; i++)
    {
        word += possibleanswers[rand() %4+1];
    }

    getline (std::cin, mystr);

    if (mystr == "you?")
        std::cout <<"I am " << word;
}
Last edited on
I am feeling free to ask mate. But I don't understand how to add the extra rows with text e.g random responses, so every time I run the code the answer is different. Thanks for helping in advance.
The key to what deathslice posted are lines 11 and 29.

Line 11 is an array of four strings each containing one word.

Line 29 chooses one of those words at random. BTW, the subscript is incorrect. It should not add 1. The entries in the array are 0-3. rand() % 4 will return a number between 0 and 3.

If you want to extend the list of words, simply extend the initializer list at line 11. And change the modulus at line 29 to be the number of entries in the list.

It's poor style to hard code the number of entries in the array as deathslice did in line 29. It's better to define a constant which contains the number of words.

1
2
3
4
5
  const int num_words = 6;
  string possibleanswers[num_words] = 
  { "good", "bad", "ugly", "tired", "sleepy", "horny" };
...
  word = possibleanswers[rand() % num_words];


The loop from lines 27-30 serves no purpose since it only executes one.


Last edited on
Kind Sir what about this line

1
2
3
    std::string mystr;
    std::string possibleanswers[] = {"good", "bad", "horrible", "fantastic"};
    std::string word;


Whatever I answer to the robot I will not receive these answers, the robot is supposed to answer with the witty texts, not me

Last edited on
The only reason I did i<1 is to guarantee only one response. So, once word receives a random entry, the loops stop and output the random entry(when I was modifying the code I kept getting multiple random entries and I assumed that the OP only wanted one witty response.). BTW thanks for the response and you're right it's bad practice to hard code anything since you always have to assume that the user before and after will be different and will create a different response.
What do you mean?The string word concatenates a random entry from the string array and the word that it gets is what the "robot" will response with.
I mean it is not supposed to have custom tailored answer to particular questions, it should just generate 100 witty responses, no matter the question, and they shouldn't be the same. ELI5, I am terrible with C++

ANY AND ALL HELP IS APPRECIATED!
Look at line 26 in your OP. The robot will only display a random response if you enter exactly "you?". You enter anything else, any the robot won;t say anything.
If you want the response to be random and different each time, then I guess you would have to check if the response elicited by the robot hasn't been outputted before and discard it if has.
Do you guys by any means know any books or tutorials that could address this case?
What do you mean by "address this case"?

Are you referring to generating random responses in general? Or are you referring to making sure the response is different each time?

If you were referring to generating random responses in general, you might google "eliza code".

If you were referring to making sure each response was unique, I would suggest loading your possible responses into a std::vector<string>. When you use a response, erase it from the vector.
Topic archived. No new replies allowed.