Sign in to follow this  

Newbie C++ Time/pause question

This topic is 4693 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently learning C++ and am still making console based applications. If I want to make the the computer display a countdown from 10 to 1 it is no problem. My question is how can I make it count down with a 1 second delay between easch number? Is there a library function I can call like TIME_PAUSE(10) for example?. Do I need to include ctime? I would appreciate any help. Thank you.

Share this post


Link to post
Share on other sites
or, you could do(this one should work on any modern OS, not just Windows):

#include <cstdlib>
#include <iostream>

int main(int argc, char* argv[]) {
for (unsigned int i = 0; i < 10; i++) {
std::cout << i << " ";
sleep(1000);
}
}


Share this post


Link to post
Share on other sites
Thank you very much.
1 more question.

Is there a function I can call to change the font color to red or yellow?

Also \n = new line and \t = tab. What is backspace so I can make the counter count in one space?


#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
for(int i = 10; i > 0; i--)
{
cout << i << [backspace here ???] ;
Sleep(1000);
}
system ("PAUSE");
return 0;
}

Share this post


Link to post
Share on other sites
There's no standard function for changing the color, although if you only need it to work on windows(at least for now), you can use conio(forgot the functions you need to use though). '\b' is the backspace character, although I'm not sure if that'll work.

Share this post


Link to post
Share on other sites

This topic is 4693 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this