Traffic light C++ problem

Thread Solved
Reply

Join Date: Feb 2009
Posts: 21
Reputation: viki0077 is an unknown quantity at this point 
Solved Threads: 0
viki0077 viki0077 is offline Offline
Newbie Poster

Traffic light C++ problem

 
0
  #1
Apr 20th, 2009
I'm really a beginner in C++ programming, and the above problem is just a part of c++ excercise. Can somebody give me a tip how to implement this in C++?
The biggest problem for me is to implement and write this:
• red+yellow light on cat traffic light is 2 sec duration
• yellow light on car traffic light is 2 sec duration
• the red on car trafffic light and red on pedestrian traffic light (the simultanuous red light) is 3 sec

How to put wait. The programme just need to write on above probem on the screen.

How can I do this?.. Please if someone can give me example or tip or write a few lines of c++ code, any help would be appreciated.

Thanks.



Traffic light table:


Car traffic light state Pedestrian traffic light state
red green
red red
red+yellow red
green red
yellow red
red red


Traffic lights work like this:
• green light on car traffic light is min. 5 secunds duration, and max Tc=30 sec.
• green light on pedestria traffic light is min. 5 secunds duration , and max. Tp=10 sec
• if there are more then 5 pedestrians on pedestrian traffic light before Tc (30 sec duration for car) the light on traffic light is changing so pedestrians can pass.
• If there are more then 5 cars on car traffic lights before Tp (min. 10 sec. For pedestrians) the light on traffic light is changing so cars may pass
• red+yellow light on cat traffic light is 2 sec duration
• yellow light on car traffic light is 2 sec duration
• the red on car trafffic light and red on pedestrian traffic light (the simultanuous red light) is 3 sec

The first state that is the 2nd state in the table (red for car and red for pedestrians).
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Traffic light C++ problem

 
0
  #2
Apr 20th, 2009
Can you show us what you've done so far?
We're not making your homework ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 21
Reputation: viki0077 is an unknown quantity at this point 
Solved Threads: 0
viki0077 viki0077 is offline Offline
Newbie Poster

Re: Traffic light C++ problem

 
0
  #3
Apr 20th, 2009
The above code is what I begin to do. I did it with switch / case, but I don' to really know how to implement this waiting (2 sec for yellow light and the other one). And I reall y don't know am I on the right way.


#include <iostream>

using namespace std;

int main (){
int selection;
float pedestriansnmb;
float carnmb;

do{
cout << "\n\nTRAFFIC LIGHT";
cout<<"\n***********************************************************";
cout << "\n1. Pedestrian number";
cout << "\n2. Number of cars)";

cout << "\n6. Exit";
cout<<"\n***********************************************************";
cout << "\nSelect: ";
cin >> selection;
cout << endl;

switch(selection){

case 1:
pedestriansnmb=0;
cout<<"\nPedestrian number is 0. It is green light for cars, and red for pedestrians.";
do{
cout<<"\n-----------------------------------------------------------";
cout<<"\nInsert number of arriving pedestrians.";
cout<<"\n-----------------------------------------------------------\n";
cin>>n;
if (n>4)
{
pedestriansnmb=pedestriansnmb+n;

cout<<"\nNumber of pedestrians on traffic light is "<< pedestriansnmb <<". It is yellow traffic light for cars, and red for pedestrians. ";
}
else
{
cout<<"\n\n-----------------------------------------------------------";
cout<<"\n Traffic light state is not changed.";
cout<<"\n-----------------------------------------------------------";
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Traffic light C++ problem

 
0
  #4
Apr 20th, 2009
Does the 2 second time gap symbolise 2 seconds or is it exactly 2 seconds?

http://www.cplusplus.com/reference/c...time/difftime/

This will help you get the 2 second delay you want. If you use the link and the example in that link properly.

After that it might be very easy.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Traffic light C++ problem

 
0
  #5
Apr 20th, 2009
You may not actually want to wait in real time. You would normally do that only for a realtime animation of the process.

It depends on what output you want. You seem to just want a line by line output of what happens after each input event. Each input event should probably have a time_delta to say whether it is for the current second (0) the next (1) or something further in the future. Each output line should start with the current_time, which would usually start at zero.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,588
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: Traffic light C++ problem

 
0
  #6
Apr 20th, 2009
having a physical delay helps demonstrate, but the delay should be significantly scaled: I'd say 1/10th. you dont want to sit at a real stoplight for 30 seconds every time you demonstrate/test the program
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 21
Reputation: viki0077 is an unknown quantity at this point 
Solved Threads: 0
viki0077 viki0077 is offline Offline
Newbie Poster

Re: Traffic light C++ problem

 
0
  #7
Apr 21st, 2009
Ok. Thank you. I'll see what can I do with this home assignement.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: Traffic light C++ problem

 
0
  #8
Apr 21st, 2009
To the OP: Please post using code tags in future ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC