New Traffic light game need some helps !!

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.

And this is my main code for rand but it is not working for the rand part.
Can anyone give me some advice??


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;
}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/204854/
This part of your code makes no sense.

1
2
3
4
5
for (int i = 0; i <= sizeArray ; i++)
	{
		numArray[i] = rand();
		cout << numArray[i] << endl;
	}


So you count through 4 times. ok. But then numArray is an array of strings and you are attempting put put a random number of type int in it. Also You only have 3 positions in the array and you are trying to fill 4. Then you output the random number in the array. You need to look at this again.
This part of your code makes no sense.

1
2
3
4
5
for (int i = 0; i <= sizeArray ; i++)
	{
		numArray[i] = rand();
		cout << numArray[i] << endl;
	}


First, you can't put random number of type int into an array of type string. Second, you're for loop counts 4 times when your array only has 3 positions. What are you trying to do in this part of your code?
Lines 21-29: You're going to have an infinite loop if the user enters something other than 's' or 'm'. You never change the value of status inside the loop.

Line 14-16 are unnecessary. You've already initialized the same values on line 12.


I think i I use the wrong concept
Is it possible if i dont use for loop and array ?
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<string>
#include<ctime>

using namespace std;

int main()

{/*
           messages are displayed.
         */
        
    }
    
    return 0;
}
Last edited on
I have used your suggestion to modify my program and it's work !
This is my program .


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
67
68
69
70
71
72
73
74
75
76
77
#include<iostream>
#include<string>
#include <stdlib.h>
#include<ctime>
using namespace std;

int selectLight()
{
    	int x = rand();
        x = rand () % 2;
        return x;
        } 

void red()
{
     cout<<"It is red light now\n";
 }   
 
 void yellow()
{
     cout<<"It is yellow light now\n";
 }
 
 void green()
{
     cout<<"It is green light now\n";
 } 




int main()

{
	
	srand(time(NULL));
	char  status;
 char light;

 for (;;)
 {

	 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'));

	 selectLight();
	 light = selectLight();
	 if (light == 0)
	 {
		 red();

	 }
	 if (light == 1)
	 {
		 yellow();
	 }

	 if (light == 2)
	 {
		 green();
	 }




	 system("pause");
 }
	return 0;
}
closed account (48T7M4Gy)
I think you need one color it seems to be working. :)
Last edited on
Thanks for your reminder !! I didn't realize it since it is a random colour. Thanks kemort ! I'm very excited because i stuck here for a long time LOL :DD
Topic archived. No new replies allowed.