Countdown program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 13
Reputation: blahblah619 is an unknown quantity at this point 
Solved Threads: 0
blahblah619 blahblah619 is offline Offline
Newbie Poster

Countdown program

 
0
  #1
Mar 14th, 2009
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!

  
  1. #include <iomanip>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #ifdef __GNUC__
  6. #include <unistd.h>
  7. #endif
  8.  
  9. #ifdef _WIN32
  10. #include <cstdlib>
  11. #endif
  12.  
  13. int main()
  14. {
  15. cout << "CTRL-C to exit...\n";
  16.  
  17. for (int tens = 10; tens > 0; tens--)
  18. {
  19. for (int units = 9; units < 10; units--)
  20. {
  21. cout << tens << '\r';
  22. cout << ' ' << units;
  23. if (units < 1)
  24. cout << '\r' << "Blast off!\n";
  25.  
  26. cout.flush();
  27.  
  28. #ifdef __GNUC__
  29. sleep(1); // one second
  30. #endif
  31.  
  32. #ifdef _WIN32
  33. _sleep(1000); // one thousand milliseconds
  34. #endif
  35.  
  36. cout << '\r'; // CR
  37. }
  38. }
  39. return 0;
  40. } // main
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 33
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: Countdown program

 
0
  #2
Mar 14th, 2009
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).
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 13
Reputation: blahblah619 is an unknown quantity at this point 
Solved Threads: 0
blahblah619 blahblah619 is offline Offline
Newbie Poster

Re: Countdown program

 
0
  #3
Mar 14th, 2009
Originally Posted by MrSpigot View Post
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):
  1. for (int tens = 10; tens > 0; tens--)
  2. {
  3. for (int units = 9; units < 10; units--)
  4. {
  5. cout << tens << '\r';
  6. cout << ' ' << units;
  7. if (units < 1)
  8. cout << '\r' << "Blast off!\n";
  9.  
  10. cout.flush();
To (New):
  1. for (int units = 10; units > 0; units--)
  2. {
  3. cout << units;
  4. if (units < 1)
  5. cout << '\r' << "Blast off!\n";
  6.  
  7. cout.flush();
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Countdown program

 
0
  #4
Mar 14th, 2009
Originally Posted by blahblah619 View Post
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):
  1. for (int tens = 10; tens > 0; tens--)
  2. {
  3. for (int units = 9; units < 10; units--)
  4. {
  5. cout << tens << '\r';
  6. cout << ' ' << units;
  7. if (units < 1)
  8. cout << '\r' << "Blast off!\n";
  9.  
  10. cout.flush();
To (New):
  1. for (int units = 10; units > 0; units--)
  2. {
  3. cout << units;
  4. if (units < 1)
  5. cout << '\r' << "Blast off!\n";
  6.  
  7. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 13
Reputation: blahblah619 is an unknown quantity at this point 
Solved Threads: 0
blahblah619 blahblah619 is offline Offline
Newbie Poster

Re: Countdown program

 
0
  #5
Mar 14th, 2009
Originally Posted by VernonDozier View Post
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!
  1. #include <iomanip>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #ifdef __GNUC__
  6. #include <unistd.h>
  7. #endif
  8.  
  9. #ifdef _WIN32
  10. #include <cstdlib>
  11. #endif
  12.  
  13. int main()
  14. {
  15.  
  16. for (int units = 10; units > 0; units--)
  17. {
  18. cout << units;
  19. if (units < 1)
  20. cout << '\r' << "Blast off!\n";
  21.  
  22. cout.flush();
  23.  
  24. #ifdef __GNUC__
  25. sleep(1); // one second
  26. #endif
  27.  
  28. #ifdef _WIN32
  29. _sleep(1000); // one thousand milliseconds
  30. #endif
  31.  
  32. cout << '\r'; // CR
  33. }
  34.  
  35. return 0;
  36. } // main
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Countdown program

 
0
  #6
Mar 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,425
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 115
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Countdown program

 
0
  #7
Mar 14th, 2009
#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

    cout << "\r  \r"; // CR
  }
  return 0; 
} // main
You can erase what's already there with some white-spaces, then print the new number.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Countdown program

 
0
  #8
Mar 14th, 2009
Originally Posted by William Hemsworth View Post
    cout << "\r  \r"; // CR
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 13
Reputation: blahblah619 is an unknown quantity at this point 
Solved Threads: 0
blahblah619 blahblah619 is offline Offline
Newbie Poster

Re: Countdown program

 
0
  #9
Mar 14th, 2009
Originally Posted by William Hemsworth View Post
#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

    cout << "\r  \r"; // CR
  }
  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!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: Arw is an unknown quantity at this point 
Solved Threads: 0
Arw Arw is offline Offline
Newbie Poster

Re: Countdown program

 
0
  #10
May 30th, 2009
Originally Posted by blahblah619 View Post
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!

  
  1. #include <iomanip>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. #ifdef __GNUC__
  6. #include <unistd.h>
  7. #endif
  8.  
  9. #ifdef _WIN32
  10. #include <cstdlib>
  11. #endif
  12.  
  13. int main()
  14. {
  15. cout << "CTRL-C to exit...\n";
  16.  
  17. for (int tens = 10; tens > 0; tens--)
  18. {
  19. for (int units = 9; units < 10; units--)
  20. {
  21. cout << tens << '\r';
  22. cout << ' ' << units;
  23. if (units < 1)
  24. cout << '\r' << "Blast off!\n";
  25.  
  26. cout.flush();
  27.  
  28. #ifdef __GNUC__
  29. sleep(1); // one second
  30. #endif
  31.  
  32. #ifdef _WIN32
  33. _sleep(1000); // one thousand milliseconds
  34. #endif
  35.  
  36. cout << '\r'; // CR
  37. }
  38. }
  39. return 0;
  40. } // main
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!

#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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC