Jump to content
  • Advertisement
Sign in to follow this  
Marz

Newbie C++ Time/pause question

This topic is 4996 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
Advertisement


#include <iostream>
#include <windows.h>

int main()
{
for(int i = 10; i > 0; i--)
{
std::cout << i << std::endl;
Sleep(1000);
}

return 0;
}

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!