Random sentance generator

How would i create a program that randomly outputs words to make a sentance. the sentance doesnt need to make sense. I have this but the only way i can think of to do something like that is srand() but i think thats only for numbers? how would i make a random sentance generator?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string W1 = "it";
    string W2 = "the";
    string W3 = "hello";
    string W4 = "way";
    string W5 = "some";
    string W6 = "can";
}
You numbered your words from 1 to 6, yet you can't figure out a way to map numbers to words?
nope
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <ctime>
#include <random>

using namespace std;

int main()
{
	srand ( time(NULL) );
	string w[6]={"it", "the", "hello", "way", "some", "can"};
	bool used[6]={false,false,false,false,false,false};
	for(int i=0;i<6;i++)
	{
		int j;
		do{
			j= rand() % 6;
		}while(used[j]!=false);
		cout << w[j] << " ";
		used[j]=true;
	}
    return 0;
}


Basically, it would best if you stored all of the strings in an array, as is done above. You then do a for loop, which will run 6 times, one for each word. It will generate a random number, and then access that element of the string array. if that element hasn't been used, it will print it. Otherwise, it will regenerate a random number and try to use a different string. Once all 6 are used, it exits.
My compiler says:

c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\c++0x_warning.h|31|error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.|
||=== Build finished: 1 errors, 0 warnings ===|


not sure what that means
Last edited on
It means if you want to use that code, add -std=c++0x to your command line when you compile.

For example,

instead of this:

g++ main.cpp

this

g++ -std=c++0x main.cpp

Alternatively, get a more recent compiler that has (better) C++11 support.
Last edited on
Ok i'll add that, but why is it giving me that error is there C++oX xode in there or something???
It's probably just because j b included <random>. Remove #include <random> and it will hopefully work.
Last edited on
I got it working, thanks :)
Topic archived. No new replies allowed.