Hey everyone,
well I wrote this program because I haven't seen anyone do it yet (it has always been random numbers, not sentences.) Also I just wanted to see if I could do it :). Im posting this not because I need it fixed, although I'll tweak it if I find a better way, I'm just wondering how YOU would create this program (produce random strings.) Im kinda postive there is a more elegant way to write this code seeing as I kinda JUST started programming.
Curious of other ways. Thanks :)

// I wrote this so I could make my conversation program
// seem less one dementional.
// And well I haven't seen anyone write a program that produced random sentences.
#include <iostream>
using namespace std;
int main()
{
    char answer='y';
    cout<<"Would you like to chat? (y/n)  ";
    cin>>answer;
    if(answer=='y')
    {
                    srand(time(0));
                    int randSent=(rand()%4+1);
                    if (randSent==1)
                    cout<<"\nSentence one goes here.\n";
                    else if (randSent==2)
                    cout<<"\nSentence two goes here.\n";
                    else if (randSent==3)
                    cout<<"\nSentence three goes here.\n";
                    else if (randSent==4)
                    cout<<"\nSentence four goes here.\n";
                                                            
    }      
    else cout<<"ok bye";
    return(0);
}

Recommended Answers

You could use a switch statement instead of an if and a bunch of else ifs - I never really got the difference but it's something for you to fool around with.

Jump to Post

All 2 Replies

You could use a switch statement instead of an if and a bunch of else ifs - I never really got the difference but it's something for you to fool around with.

Personally I would put the sentences in an array and then use rand to pick which one because an array is numerically ordered

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.