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!
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.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
usingnamespace std;
int main()
{
constint 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;
}
}