| | |
Traffic light C++ problem
Thread Solved
![]() |
•
•
Join Date: Feb 2009
Posts: 21
Reputation:
Solved Threads: 0
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).
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).
•
•
Join Date: Feb 2009
Posts: 21
Reputation:
Solved Threads: 0
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-----------------------------------------------------------";
}
#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-----------------------------------------------------------";
}
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.
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.
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.
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.
![]() |
Similar Threads
- Java code to display a GUI (Java)
- tkinter loop problem (Python)
- tkinter problem (Python)
- Problem VIRUS ALERT (Viruses, Spyware and other Nasties)
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Dreaded IDE #1 ERROR info. (Storage)
- Traffic Light (Java)
- Remote File Access (Network Security)
- Ethernet Adapter problem? (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: Compiling C++
- Next Thread: Deleting Structured Array Entry
| Thread Tools | Search this Thread |
anyfile api array based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download draw dynamic dynamiccharacterarray email encryption error file forms fstream function functions game gdi+ givemetehcodez gnu graph gui homeworkhelp homeworkhelper http iamthwee ibm ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output packing parameter pointer problem program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video visualc++ visualizationtoolkit win32 windows winsock word wordfrequency wxwidgets






