| | |
Countdown program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2009
Posts: 13
Reputation:
Solved Threads: 0
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!
Please help!
Thanks!
C++ Syntax (Toggle Plain Text)
#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).
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).
•
•
Join Date: Feb 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
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).
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):
C++ Syntax (Toggle Plain Text)
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();
C++ Syntax (Toggle Plain Text)
for (int units = 10; units > 0; units--) { cout << units; if (units < 1) cout << '\r' << "Blast off!\n"; cout.flush();
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
•
•
•
•
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):
To (New):C++ Syntax (Toggle Plain Text)
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();
C++ Syntax (Toggle Plain Text)
for (int units = 10; units > 0; units--) { cout << units; if (units < 1) cout << '\r' << "Blast off!\n"; cout.flush();
•
•
Join Date: Feb 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#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
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
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.
On a separate issue, I don't think your "Blast Off" is going to ever print. Try putting it AFTER the countdown loop.
•
•
Join Date: Mar 2008
Posts: 1,425
Reputation:
Solved Threads: 115
#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;
} // mainHope this helps.
•
•
Join Date: Jan 2008
Posts: 3,818
Reputation:
Solved Threads: 501
•
•
•
•
You can erase what's already there with some white-spaces, then print the new number.cout << "\r \r"; // CR
Hope this helps.
•
•
Join Date: Feb 2009
Posts: 13
Reputation:
Solved Threads: 0
•
•
•
•
You can erase what's already there with some white-spaces, then print the new number.#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
Hope this helps.
I printed Blast off at the end. It works now!
•
•
Join Date: May 2009
Posts: 5
Reputation:
Solved Threads: 0
•
•
•
•
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!
C++ Syntax (Toggle Plain Text)
#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
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
![]() |
Similar Threads
- Visual Basic Timer/Clock/Countdown. (Visual Basic 4 / 5 / 6)
- Can Any One Help Me With A CountDown Timer (Visual Basic 4 / 5 / 6)
- VB6 to play MP3 at certain point of countdown (Visual Basic 4 / 5 / 6)
- MFC Countdown Timer (C)
- Timer Countdown (C++)
- Countdown to a date (Visual Basic 4 / 5 / 6)
- Countdown (Java)
Other Threads in the C++ Forum
- Previous Thread: System Resource Manipulating Script.
- Next Thread: Visual C++ Networking
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






