I'm trying to get this countdown to work. I'm trying to get it to countdown from 10 down to "Blast off", with Blast off being 0. Each output replaces the one before it. I'm very confused. 10 does not print, but 9 to Blast off do. When when it Blast off prints, it keeps printing. Does anyone know how I can print 10 and also print Blast off one time?
Please help!
Thanks!

#include <iomanip>
#include <iostream>
using namespace std;

#ifdef __GNUC__
#include <unistd.h>
#endif

#ifdef _WIN32
#include <cstdlib>
#endif

int main()
{
  cout << "CTRL-C to exit...\n";

      for (int tens = 10; tens > 0; tens--)
      {
        for (int units = 9; units < 10; units--)
        {
          cout << tens << '\r';
      cout << ' ' << units;
      if (units < 1)
      cout << '\r' << "Blast off!\n";

      cout.flush();

          #ifdef __GNUC__
          sleep(1); // one second
          #endif

          #ifdef _WIN32
          _sleep(1000); // one thousand milliseconds
          #endif

          cout << '\r'; // CR
        }
      }
  return 0; 
} // main

If you're just counting down from 10 to 0 I'm not exactly sure why you've used 2 loops. Surely a single loop would do the job.
I do notice that the inner loop won't terminate because your condition (units < 10) is incorrect. units is counting down and will always be less than 10 (or at least for a VERY long time).

If you're just counting down from 10 to 0 I'm not exactly sure why you've used 2 loops. Surely a single loop would do the job.
I do notice that the inner loop won't terminate because your condition (units < 10) is incorrect. units is counting down and will always be less than 10 (or at least for a VERY long time).

Alright. I now have one loop and the program ends, but now it prints(still replacing each number):
10
90
80
70
60
50
40
30
20
10
(program ends)

I don't know how to get rid of the zero's and print "Blast off!"
Here's the code block I changed:
From this (Old):

for (int tens = 10; tens > 0; tens--)
      {
        for (int units = 9; units < 10; units--)
        {
          cout << tens << '\r';
	  cout << ' ' << units;
  	  if (units < 1)
  	  cout << '\r' << "Blast off!\n";

	  cout.flush();

To (New):

for (int units = 10; units > 0; units--)
        {
	  cout << units;
  	  if (units < 1)
  	  cout << '\r' << "Blast off!\n";

	  cout.flush();

Alright. I now have one loop and the program ends, but now it prints(still replacing each number):
10
90
80
70
60
50
40
30
20
10
(program ends)

I don't know how to get rid of the zero's and print "Blast off!"
Here's the code block I changed:
From this (Old):

for (int tens = 10; tens > 0; tens--)
      {
        for (int units = 9; units < 10; units--)
        {
          cout << tens << '\r';
	  cout << ' ' << units;
  	  if (units < 1)
  	  cout << '\r' << "Blast off!\n";

	  cout.flush();

To (New):

for (int units = 10; units > 0; units--)
        {
	  cout << units;
  	  if (units < 1)
  	  cout << '\r' << "Blast off!\n";

	  cout.flush();

Post the entire updated program please so we know for sure what has been changed and what hasn't. I can't see why the updated code would result in that output.

Post the entire updated program please so we know for sure what has been changed and what hasn't. I can't see why the updated code would result in that output.

Here's the program. I'm so confused!

#include <iomanip>
#include <iostream>
using namespace std;
 
#ifdef __GNUC__
#include <unistd.h>
#endif
 
#ifdef _WIN32
#include <cstdlib>
#endif
 
int main()
{
 
        for (int units = 10; units > 0; units--)
        {
	  cout << units;
  	  if (units < 1)
  	  cout << '\r' << "Blast off!\n";

	  cout.flush();
        
          #ifdef __GNUC__
          sleep(1); // one second
          #endif
 
          #ifdef _WIN32
          _sleep(1000); // one thousand milliseconds
          #endif
 
          cout << '\r'; // CR
        }

  return 0; 
} // main

Looks to me like the 0's that are being displayed are from the original 0 in the 10 and that 0 isn't being overwritten and thus remains. Change it so it counts down from 9 to 0 rather than 10 to 0 and see if that extra 0 goes away. Also, temporarily change from displaying '\r' to displaying '\n' and see if it counts down correctly. I think this is a display issue, not a code/loop logic issue. I'm not very familiar with how '\r' displays, nor with how sleep works, but I'd say it has to do with using '\r' rather than '\n'.

On a separate issue, I don't think your "Blast Off" is going to ever print. Try putting it AFTER the countdown loop.

#include <iomanip>
#include <iostream>
using namespace std;

#ifdef __GNUC__
#include <unistd.h>
#endif

#ifdef _WIN32
#include <cstdlib>
#endif

int main()
{
  for (int units = 10; units > 0; units--) {
    cout << units;

    if (units < 1)
      cout << "\rBlast off!\n";

    cout.flush();

#ifdef __GNUC__
    sleep(1); // one second
#endif

#ifdef _WIN32
    _sleep(1000); // one thousand milliseconds
#endif

    [B]cout << "\r  \r"; // CR[/B]
  }
  return 0; 
} // main

You can erase what's already there with some white-spaces, then print the new number.

Hope this helps.

[B]cout << "\r  \r"; // CR[/B]

You can erase what's already there with some white-spaces, then print the new number.

Hope this helps.

Yep, that's what you want. You still have the "Blast Off" problem, but that'll take care of the 0.

#include <iomanip>
#include <iostream>
using namespace std;

#ifdef __GNUC__
#include <unistd.h>
#endif

#ifdef _WIN32
#include <cstdlib>
#endif

int main()
{
  for (int units = 10; units > 0; units--) {
    cout << units;

    if (units < 1)
      cout << "\rBlast off!\n";

    cout.flush();

#ifdef __GNUC__
    sleep(1); // one second
#endif

#ifdef _WIN32
    _sleep(1000); // one thousand milliseconds
#endif

    [B]cout << "\r  \r"; // CR[/B]
  }
  return 0; 
} // main

You can erase what's already there with some white-spaces, then print the new number.

Hope this helps.

Thank you VERY much!
I printed Blast off at the end. It works now!

I'm trying to get this countdown to work. I'm trying to get it to countdown from 10 down to "Blast off", with Blast off being 0. Each output replaces the one before it. I'm very confused. 10 does not print, but 9 to Blast off do. When when it Blast off prints, it keeps printing. Does anyone know how I can print 10 and also print Blast off one time?
Please help!
Thanks!

#include <iomanip>
#include <iostream>
using namespace std;

#ifdef __GNUC__
#include <unistd.h>
#endif

#ifdef _WIN32
#include <cstdlib>
#endif

int main()
{
  cout << "CTRL-C to exit...\n";

      for (int tens = 10; tens > 0; tens--)
      {
        for (int units = 9; units < 10; units--)
        {
          cout << tens << '\r';
      cout << ' ' << units;
      if (units < 1)
      cout << '\r' << "Blast off!\n";

      cout.flush();

          #ifdef __GNUC__
          sleep(1); // one second
          #endif

          #ifdef _WIN32
          _sleep(1000); // one thousand milliseconds
          #endif

          cout << '\r'; // CR
        }
      }
  return 0; 
} // main

end quote.

Hi
Well I have tried to modify your program. It's true I don't really know much about some of the code you used and I am considered to be a beginner in programming, but I try. I used only one loop instead of two. And since your in your algorithm each output replaces the one before, the only way to make it look right was to write digits less than 10 as(09, 08, 07,....)
I really hope I helped!:icon_wink:

#include <iomanip>
#include <iostream>
using namespace std;
 #ifdef __GNUC__
#include <unistd.h>
#endif
 #ifdef _WIN32
#include <cstdlib>
#endif
 int main()
{
    cout << "CTRL-C to exit...\n";
    int units = 10;
    cout << ' ' << units;
    _sleep(1000);
     for (units = 9; units >= 0; units--)
     {
        cout << '\r' << units << '\r';
        cout << ' ' << '0' << units << '\r';
        if (units == 0)
            cout << '\r' << "Blast off!\n";
        cout.flush();
        #ifdef __GNUC__
        sleep(1); // one second
        #endif
        #ifdef _WIN32
        _sleep(1000); // one thousand milliseconds
        #endif
        cout << '\r'; // CR

      }

  return 0; 
} // main

You just replied to an already solved, year old thread. Despite that, you failed to use code-tags.

You just replied to an already solved, year old thread. Despite that, you failed to use code-tags.

I know :$ . I was searching the Net for countdown codes and this thread was one of the search results. So, it showed 0 replies. After posting my reply, I dicovered that it was already solved by many of you clever programmers. Foregive me. However, I know it doesn't matter now, but there are many lines that are not really needed in the code. This one performes the countdown just fine:

#include<iostream>
#include<windows.h>
using namespace std;

int main()
{
    cout << "\n\n\t  ---------------------\n";
    cout << "\t    !! COUNTDOWN !!\n\n";
    cout << "\r\t        >> ";
    for (int units =10; units >= 0; units--)
    {   
        cout << units;
        if (units == 0)
        {
            cout << "\r\t     >> Blast off!\n\n";
            return 0;
        }

        Sleep(1000); // one thousand milliseconds
        cout << "\r  \r\t        >>  "; // CR
    }
        return 0;
}

SORRY AGAIN!

Arw: Click Here

>SORRY AGAIN!
Stop posting code to a solved thread (without code-tags), and you wont need to say sorry.

>Arw: Click Here
What if someone doesn't have a Flash Player. He won't fail LOL.
This happened to me while I was viewing it on Lynx(Text based web browser)
Anyways. Quite nice link.

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.