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)  ";
                    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";
7 Years
Discussion Span
Last Post by kolosick.m188

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.