How can I make a random response generator?

Pages: 12
I'm writing this program, and I want to know if there is a way to have it display "good" or "bad" depending on a random response generator. Thanks.


#include <iostream>
#include <string>
using namespace std;

int main ()
{

string mystr;
cout <<"What is your name?"; //name
getline (cin, mystr);
cout <<"Hello " << mystr << ", nice to meet you.";
cout <<" How are you today?"; //how are you
getline (cin, mystr);
if (mystr == "good")
{
cout <<"That is good.";
}
else if (mystr == "bad")
{
cout <<"That isn't good.";
}

else
{
cout <<"That's interesting.";
}
getline (cin, mystr);
if (mystr == "you?")
{
IT WOULD GO HERE
}
Random? Use rand() (or maybe it was std::rand().) An srand(time()) at the beginning of the program should help.
What do you mean by random response? The code works fine minus the missing end } for main(). Maybe I'm missing the concept.
my skype is:magas1291
i can help you with your programs.
-) As whovian suggsted, use rand() to generate a random number

-) You can translate that random number to a string by having a lookup table of possible answers:

1
2
3
4
5
6
const char* possibleanswers[5] = {
 "here", "are", "some", "possible", "answers"
};

// print one of the above 5 words at random:
cout << possibleanswers[ rand() % 5 ];
Lisp :D
Thanks. Disch, how would I put that into there? Sorry I'm new...
Okay, what is wrong with this? if (mystr == "you?")
{
rand();
{
const char* possibleanswers[2] = {
"good", "bad"};
}
cout <<"I am " <<possibleanswers [rand() %2];
}
It says that rand and possibleanswers weren't declared.
Why do you have those random braces around the possibleanswers declaration? And are you including the header with rand() in it?
Did you initialize random seed? Try something like this.
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
#include <iostream>
#include <string>
#include <ctime>

int main ()
{
    srand(static_cast( 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 << " How are you today?"; //how are you
    getline(std::cin, mystr);
  
    if(mystr == "good")
        std::cout <<"That is good.";
    else if(mystr == "bad")
        std::cout <<"That isn't good.";
    else
        std::cout <<"That's interesting."


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

    return 0;
} 


[Edit]: I encourage you to perfect your program and make it better! For instantance, what if the user enters, "Good." Your program would display, "That's interesting." You can easily add something to make the program more interactive and it can help your programming a lot.

To make it even more challenging, what if that says something like, "I am good."

And like Zhuge said, you seem to have a lot of random errors about your program. And it doesn't look like you use any syntax editor. Using one many improve yourself with you error mistakes. Some white spacing is what you need, so you understand where your loops and sections open and close.
Last edited on
Thanks a bunch!
Sorry, one last thing, what is wrong with this? I put in exactly as above.


#include <iostream>
#include <string>
#include <ctime>
using namespace std;

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

string mystr;
std::cout <<"What is your name?"; //name
getline (cin, mystr);
std::cout <<"Hello " << mystr << ", nice to meet you.";
std::cout <<" How are you today?"; //how are you
getline (cin, mystr);
if (mystr == "good")
{
std::cout <<"That is good.";
}
else if (mystr == "bad")
{
std::cout <<"That isn't good.";
}

else
{
std::cout <<"That's interesting.";
}
getline (cin, mystr);
if (mystr == "you?")
{
std::cout <<"I am " <<possibleanswers[rand() %2];
}
Also, it says that
expected < before (
expected type specifier before (
expected > before (
srand wasn't declared
expected initializer before std
and possibleanswers wasn't declared


some final help please? Thanks

You're missing you closing } for main()

You also never defined possible answers.
1
2
3
4
5
6
const char* possibleanswers[5] = {
 "here", "are", "some", "possible", "answers"
};

// print one of the above 5 words at random:
cout << possibleanswers[ rand() % 5 ];


And I forget what header you need for srand, but I believe it's #include<random>
Last edited on
You can almost completely ignore my previous comment. You did forget you closing } for main(), and srand needs #include<stdlib.h>. You also missed a semicolon after your FIRST declaration of mystr, and had a second declaration as well. You also declared "using namespace std" but never used the namespace implicitly, you always called it directly, so I removed that and plugged in the std:: at the three cin's. I cleaned up your other code, formatted it, and this is how it looks.
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
#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 <<" How are you today?"; //how are you

    getline (std::cin, mystr);
    if (mystr == "good")
        std::cout <<"That is good.";
    else if (mystr == "bad")
        std::cout <<"That isn't good.";
    else
        std::cout <<"That's interesting.";

    getline (std::cin, mystr);
    if (mystr == "you?")
        std::cout <<"I am " << possibleanswers[rand() %2];
}
Last edited on
what if I wanted to put the possibleanswers somewhere else as well? How would I do that?
I assume you'd like to have a random response for, lets say, when the user asks where you're from. possibleanswers could be like "My creator", "My mom", or "I just happened".
you could define another set of answers at the top like so:
std::string randWhereFrom[3] = {"My Creator", "My Mom", "I just happened"};
and then you could add it after the last if statement like so:
1
2
3
getline (std::cin, mystr);
if (mystr == "Where are you from?")
   std::cout << randWhereFrom[rand() % 3];


To learn more about what's going on here is that when you call the function rand() it returns a random number based on the seed given by srand(). This number can be a huge number, but you only care about numbers 1-x, in my example, 1-3. The modulus, %, returns the remainder when divided by x, in my example, a random number is divided by 3 and you will get either a remainder of 0, 1, or 2. That number corresponds to the position in the array of randWhereFrom.

Understand?
alright, so I just put the std::string randWhereFrom[3] = {"My Creator", "My Mom", "I just happened"} or whatever it is at the time, before the question?
Yeah, as long as you declare it before you use it, you're fine, but I suggest trying to keep your code organized, you'll thank yourself for it once you get some really long programs.
Pages: 12