Feb 21, 2016 at 7:22pm UTC
Hi,
I'm making a program which will generate random words and the words shouldn't repeat. I'm new in programming and I would like to get some 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
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
int main()
{
int word[4];
srand(time(0));
for (int x = 0; x<5; x++){
word[x] = 1+rand()%5;
if (word[x]==1){
cout<<"sky" <<endl;
}
if (word[x]==2){
cout<<"tree" <<endl;
}
if (word[x]==3){
cout<<"house" <<endl;
}
if (word[x]==4){
cout<<"car" <<endl;
}
if (word[x]==5){
cout<<"speed" <<endl;
}
}
return 0;
}
Soo how I can make conditions for the words.
(example: The program can repeat the word "speed" maximum 2 times, so the words can't be : 1-car 2-speed 3-tree 4-speed 5-speed etc..)
Last edited on Feb 21, 2016 at 7:23pm UTC
Feb 21, 2016 at 7:40pm UTC
You can create an array which stores the number of time each word appears.
Feb 21, 2016 at 8:00pm UTC
Do as KMagic said. Don't forget to check whether a word has been called twice.
Feb 21, 2016 at 8:39pm UTC
Can anyone give me an example, please ?