How can I display a random sentence?

Hello, I am new to C++ and I have got the following program running fine and doing what I want so far. However, is there anyway I can produce a random sentence as the output for each location that is chosen. Because at the moment if I keep entering the same number, the same sentence is displayed but I want a random sentence to appear rather than the same one. Appreciate the help!

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


int main()
{

	cout << "Please select one of the locations:" << endl;
	cout << endl;
	int lChoice;


	do 
	{
		cout << "1) Manchester" << endl;
		cout << "2) Scotland" << endl;
		cout << "3) London" << endl;
		cout << "4) Essex" << endl;
		cout << "5) Birmingham" << endl;
		cout << "6) Bolton" << endl;
		cout << "7) Exit" << endl;
		cin >> lChoice;

		if(lChoice == 1)
		{
			cout << "Trains are delayed" << endl;
		}

		else if(lChoice == 2)
		{
			cout << "All travel services are on schedule - enjoy your journey!" << endl;
		}

		else if(lChoice == 3)
		{
			cout << "Flight currently delayed for 30-45 minutes" << endl;
		}

		else if(lChoice == 4)
		{
			cout << "All travel services are on schedule - enjoy your journey!" << endl;
		}

		else if(lChoice == 5)
		{
			cout << "Flights delayed for around 2 hours due to storm" << endl;
		}

		else if (lChoice == 6)
		{
			cout << "All travel services are on schedule - enjoy your journey!" << endl;
		}

		else
		{
			cout << "Please select a valid location" << endl;
		}
	}

	while (lChoice != 7);


    return 0;
}
Last edited on
If you want random sentences you don't need a switch, just store all the messages in a std::vector<std::string>, std::random_shuffle them every time user inputs a number and then display any element of the vector, either a pre-determined element (first, last etc) or just pick a random element from a random shuffled vector
Well, the one for Manchester is so accurate I can't see why you would want to change it. However ...

Have a representative set of travel announcements (see below) in a string array and choose the index at random using rand() (or some more modern random-number generator). Make sure you seed it first with srand() or you will get the same sequence.
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
   const int NMAX = 7;
   int r;
   string message[NMAX] = { 
                            "All trains running on time",
                            "No trains going to the airport",
                            "Rail-replacement buses are in operation from platform 2",
                            "A broken-down train is delaying all departures",
                            "Your call is very important to us",
                            "We're currently waiting for an engineer",
                            "Due to over-running engineering works ..."
                          };

   srand( time(0) );

   char ans = 'y';
   while ( ans == 'y' || ans == 'Y' )
   {
      r = rand()%NMAX;
      cout << message[r] << endl;
      cout << "Another? (y/n)";
      cin >> ans;
   }
}
Last edited on
Haha, trains and their schedules ey!

But thanks for the help! I appreciate it :)
Topic archived. No new replies allowed.