DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Traffic light C++ problem (http://www.daniweb.com/forums/thread187535.html)

viki0077 Apr 20th, 2009 10:10 am
Traffic light C++ problem
 
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).

tux4life Apr 20th, 2009 12:07 pm
Re: Traffic light C++ problem
 
Can you show us what you've done so far?
We're not making your homework ...

viki0077 Apr 20th, 2009 2:45 pm
Re: Traffic light C++ problem
 
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-----------------------------------------------------------";
}

Sky Diploma Apr 20th, 2009 2:53 pm
Re: Traffic light C++ problem
 
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.

nucleon Apr 20th, 2009 3:34 pm
Re: Traffic light C++ problem
 
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.

jephthah Apr 20th, 2009 4:08 pm
Re: Traffic light C++ problem
 
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 :P

viki0077 Apr 21st, 2009 10:13 am
Re: Traffic light C++ problem
 
Ok. Thank you. I'll see what can I do with this home assignement.

tux4life Apr 21st, 2009 1:52 pm
Re: Traffic light C++ problem
 
To the OP: Please post using code tags in future ...


All times are GMT -4. The time now is 9:42 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC