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).

Recommended Answers

All 7 Replies

Can you show us what you've done so far?
We're not making your homework ...

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-----------------------------------------------------------";
                                }

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.

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

Ok. Thank you. I'll see what can I do with this home assignement.

To the OP: Please post using code tags in future ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.