Traffic light game need helps !!



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
This is what I need to do . I had goggle what is rand and srand but what i get is the rand and rand only showed random input number.
But what i read from the question is the light of the traffic need to be random.
So can the WORDS be random by using rand and srand ?
I'm struggle on this for few days. Can anyone give me some advice or hint how should i start ? 
Thanks for your help !!!

Your program should generate status of the traffic light (“red”/ “green”/ “yellow”) randomly
to simulate its operation status. (Hint: apply rand( ) and srand( ) function)
Player will be prompted to enter his current car status either it is in “moving mode” or in
“stop mode”.
For example,
Enter your car status: (s-STOP/ m-MOVING):
Based on the player input and the traffic light status, your program should check if the player
breaks the traffic regulation. By default, each player will be given 10 marks once the game is
started. Player’s score will be decremented by 3 points if he breaks the regulation. In contrast,
he will be awarded 1 point if he obeys the regulation. However, no points will be deducted or
awarded if the traffic light is “yellow” regardless of the player’s car status.
Each player will be given 10 points as the game starts. If the user’s points is less than or equal
to zero, prompt the user by displaying relevant messages and terminate the game
automatically.
Display all possible informative messages based on different scenarios. Your program should
also calculate the player’s score and display his latest score on the screen. E.g. if the car is
moving when the light is “red”, display the message as follows:
Your car is moving.
The light is red.
Behave yourself!
Your score is decremented by 3 point!
Your latest score is 7. 
closed account (48T7M4Gy)
In general terms if you store the words in an array, by producing random digits as index values, you can use those index values to access the array.
I'm sorry.
I don't really understand what are you trying to tell me. Is it using the index value to represent the words/alphabet ?
do you mind to show me some example ?
closed account (48T7M4Gy)
Lets say the words are red yellow green.

So string word[] = {"red", "yellow", "green)
That means word[0] = "red", etc up to word[2]

So if you generate a random number between 0 and 2 inclusive you can call up a random word.

eg random number generator gives you index = 1, then word[1] = "yellow" is the randomly selected word. Bingo!
I think i get it !
I will try to write my code now ! Thanks !!
closed account (48T7M4Gy)
OK, it's not too difficult - just a few lines of code is all you need to set it up :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<string>
#include <stdlib.h>
using namespace std;
int main()

{
	string word[] = { "red", "yellow", "green" };

	for (int word = 0; word <= 2; word++)
	{
		cout << rand << endl;
	}
}


Is it something like this ? i still cant get after tried so many times
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
#include<iostream>
#include<string>
#include <stdlib.h>
#include<ctime>
using namespace std;
int main()

{
	
	srand(time(NULL));
	const int sizeArray = 3;
	string numArray[sizeArray] = { "red", "yellow", "green" };
	char  status;
	numArray[0] = "red";
	numArray[1] = "yellow";
	numArray[2] = "green";
	


	cout << "Enter your status of your car:\n";
	cin >> status;
	do
	{

		if (status == 's')
			cout << "Your car is not moving.\n";
		if (status == 'm')
			cout << "Your car is moving.\n";
	} while (!(status == 's' || status == 'm'));

	for (int i = 0; i <= sizeArray ; i++)
	{
		numArray[i] = rand();
		cout << numArray[i] << endl;
	}
	system("pause");
	return 0;
}


This is my code and it is not running for my rand
can anyone give me some advice?
line 33 - rand() is returning a number and the code is trying to assign this number directly to an array that contains strings. As Kemort mentioned above, the random number could be used as an index into the array, i.e. the [i] in numArray[i].

Since you only want a small subset of random numbers (0, 1 or 2), use the % modulus operator in conjunction with rand() to limit the possible values.

http://www.cplusplus.com/reference/cstdlib/rand/
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/204868/
Topic archived. No new replies allowed.