ok iam working on a password protection for my program but i have a problem.....
if the user set wrong password the program do this

Wrong password!!
The program will be terminated in 5 seconds

but i want sec, (which is my seconds) at every second - 1
5-4-3-2-1 without present all the "the program will......"

here is my code:::

int main() //main
{
system("color 01c");
char chose;
char option;
int i,sec=5;
string password;

cout<<"THIS PROGRAM IS LOCKED!!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<"PASSWORD: ";
getline (cin,password);

if (password == "my pass")
{
menu();
cout << "Your Choice: ";
cin >> chose;

if (chose == '1')
{
menu2();
}
else 

if (chose == '2')

menu3();
}
else 
for (i=0; i<=5; i++)
{
cout <<"WRONG PASSWORD!!"<<endl;
cout <<"The program will be terminated in "<<sec--<<" seconds...";
Sleep(5000);
return 0;
} 
char cChar;
std::cin.get(cChar);
return 0;
}

Use a loop to count down:

cout <<"The program will be terminated in ";
for(int i = 5; i > 0; --i)
{
    i << " seconds..." << endl;
    Sleep(1000);
}

Just use a for loop (which runs 5 times) ...
In the while loop you place the instruction sleep(1000); and you print out the loop index everytime on the screen ...

Something like this:

...
cout << "The program will end in ";
for(int i = 5; i > 0; i--)
{
    cout << i << "seconds ..." << endl;
    sleep(1000);
}
...

Edit:: I was posting at the same time lerner did ...

I think their is other way that I could make this, without write the program again and again the seconds....

for example,
every second it should not written again and again, but replace the older second... with the new one,, so lets say that we start with 5
program write 5 and when the subtraction is done, program replace the 5 with 4 without written the 5..... so i want a way that program didn't write again and again the numbers but replace the old number with the new....

thank you very much for your help!!!!!!!!

also with that code

cout << "The program will end in ";
for(int i = 5; i > 0; i)
{
cout << i-- << " seconds..." << endl;
Sleep(1000);

}

the program didn't exit but pause because Sleep is in the loop

Add the instruction return 0; just after the for-loop ...

Make use of character escape sequences like the following example:

#include <iostream>

using namespace std;

int main()
{
    cout << "The program will be terminated in: ";
    for(int i = 5; i > 0; i--)
    {
	cout << i << " seconds ...";
	cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b";
	sleep(1000);
    }
    cout << endl;
    return 0;
}

tux4life thank you very much for your help :D:D:D

Can you explain why you have invented so strange manner to die for your top secret application? ;)...

Can you explain why you have invented so strange manner to die for your top secret application? ;)...

:P

SOUNDS LIKE MISSION IMPOSSIBLE.

This will self destruct in 5-4-3-2-1 BOOM!!! :)

SOUNDS LIKE MISSION IMPOSSIBLE.

This will self destruct in 5-4-3-2-1 BOOM!!! :)

Haha :P, I actually don't understand why it's that important, he could simply display a message and close the program ...

:P Ιs not something important, by the way the code is not crypted,so anyone can find the pass at 1 sec if he/she know of programing...
sooo " it's only for me " ....

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.