i would like help making a little piece of code that just counts down from a number, example 30 to 0. the program shouldnt display the previous numbers counted. thats it, any hel-p would be great.

so this is all part of a larger project of mine. the teacher told us to make a program that simulates a stop light/ intersection, we have to use ascii to "draw" the intersection then have the stop leights as numbers that change color based on wether the light is supposed to be red green or yellow. as you can see im not very far in the project ive only just got the ascii done and im kinda stuck on the countdown for each of the lights.

//traffic control
#include <iostream>
#include <Windows.h>
using namespace std;
int main()
{

    cout <<"welcome to the traffic control simulation" <<endl;
    Sleep (1000);
    cout <<"let us begin" <<endl;
    Sleep (2000);
    cout <<"       |       |       " <<endl;

    cout <<"       |       |       " <<endl;

    cout <<"-------|-------|-------" <<endl;

    cout <<"       |       |       " <<endl;

    cout <<"       |       |       " <<endl;

    cout <<"-------|-------|-------" <<endl;

    cout <<"       |       |       " <<endl;

    cout <<"       |       |       " <<endl;


    return 0;
}

the program shouldnt display the previous numbers counted.

Do I take this to mean that there should be no output? Or should there be a countdown from 30 to 0, with a delay in between each iteration, then there is a "Screen Clear" and the new number is displayed? Something else? "Screen Clear" is in quotes because there are different ways of simulating it in a console program, two common ways being to display a bunch of newlines or executing a system("CLS") command. Someone might chime in saying not to use system("CLS") and there are good reasons. Consider a search of the forum regarding the "system" command, portability, etc., and ask your teacher how the screen should be cleared, if indeed it should.

Now to your main question. The program flow is going to go something like this.

for(int i = 30; i >= 0; i--)
{
    // clear screen
    // display i and anything else you need to display using cout
    // pause
}
Comments
thankyou i think this is what i need ill be talk more when i have put it into my current code

is there any way that i could have the clear screen just clear inbetween each number? and to only clear the number?

You don't have to clear the screen, just move the cursor back to the beginning of the line

cout << "\r" << number;

That will work only if you want to print the number at the beginning of the line.

To overwrite the same line you use the '/r' option in your cout function.

 int seconds=1440;
 for (;seconds--;){
    cout << '\r' << seconds / 60 << ":" << setw(2) << seconds % 60;
    Sleep(1000);
 }

Edited 3 Years Ago by Lucaci Andrew: Actually, I won't give you the whole code.

You don't have to clear the screen, just move the cursor back to the beginning of the line

True enough at this stage of the project and I hadn't thought of the "\r" option. It's going to get complicated soon though and quite a few things are going to be drawn, so might as well get the Clear Screen going. He'll need to do it eventually.

we have to use ascii to "draw" the intersection then have the stop leights as numbers that change color based on wether the light is supposed to be red green or yellow.

Toss out the potential portability concerns. This isn't going to be portable no matter what with the colors. Figure out what you need to use for the colors. I'm sure it will be some library. That library may have other options yuou can use. It all gets you farther and farther away from regular old standard C++.

    int counter = 30;

    // "While 'counter' is going to zero".
    while (counter --> 0)
    {
        // Do things on every decrement, if desired.
    }

=P

Edited 3 Years Ago by Gonbe

This article has been dead for over six months. Start a new discussion instead.