Need help to ouput premade lines of text but randomly!!!

Jul 28, 2017 at 8:23pm
Can anyone help, I need to randomly output for example

1
2
3
4
5
6
  possible sentences that will output: (///no limit and all random each time

   How are you? What's your name? what day is it? etc

Single sentences not all three, but different each time :)
 


This code will be in a loop which will break after outputting, and when its called again its expected to be random!!!


I am not asking to display randomly generatd sentneces, but instead sentences which I made previously to be randomly outputted.

Last edited on Jul 28, 2017 at 8:24pm
Jul 28, 2017 at 8:40pm
You can use random number generator with rand()(http://www.cplusplus.com/reference/cstdlib/rand/ ) function and an array of strings which will store your possible snetences. Then you just generate random number and print the string with index of this number from the array.
Last edited on Jul 28, 2017 at 9:09pm
Jul 28, 2017 at 8:53pm
Hi Tikeri thanks for the reference, the page is showing as not found, your idea sounds good :)

Could you possibly demostrate this? if not, its ok.
Last edited on Jul 28, 2017 at 8:53pm
Jul 28, 2017 at 9:04pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
   vector<string> sentences = { "Great Expectations", "A Tale of Two Cities", "Hard Times",
                          "The Old Curiosity Shop", "Oliver Twist", "Martin Chuzzlewit",
                          "Pickwick Papers", "David Copperfield", "Dombey and Son",
                          "A Christmas Carol" };

   int size = sentences.size();
   string dummy;
   srand( time( 0 ) );
   cout << "Keep pressing enter\n";
   while ( getline( cin, dummy ) ) cout << sentences[rand()%size] << '\n';
}
Last edited on Jul 28, 2017 at 9:09pm
Jul 28, 2017 at 9:11pm
Thanks very much lastchance, I am going to try this out tomorrow, this has been wreaking my head for sometime now, your help is much appreciated thanks again :)
Jul 28, 2017 at 9:15pm
I'm sorry for the broken link, I corrected it now and the demonstration is already here thanks to lastchance. So I think you got over the hump. ;)
Jul 29, 2017 at 10:04pm
@ Tikeri, that's ok, and thanks again for your help :)

@ lastchance, works perfectly, thanks very much, much appreciated :)
Topic archived. No new replies allowed.