A progress bar is the bar, usually green, appearing in myriad operating systems and programs when something is loading. Today, we're going to put matters to upload files or anything, just let us see the basic principle on which they operate these bars.
Our program will only ask a number in seconds that will be the last time to fill our progress bar. In the window where you run our programs fit 80 characters long, so let's say that our progress bar has a resolution of 80. The only problem is knowing how long each unit of those 80 to accumulate the total time indicated.
The function delay () or Sleep ()
If your IDE can use the conio.h library, then they will use the function delay (). If not, we will have to add the library function windows.h and use Sleep () (well capitalized).
Both functions do exactly the same: stop the program for a certain amount of time in milliseconds. For example:
It shows 'Hello', pauses 2 seconds and displays 'hello you two seconds ago!'
cout << "Hola" << endl;
Sleep(2000);
cout << "Hace dos segundos te saludé!";
It shows 'Hello', pauses 2 seconds and displays 'hello you two seconds ago!'
For those who use conio.h, it would be something like:
printf("Hola\n");
delay(2000);
printf("Hace dos segundos te saludé!");
Now, to find out how long each unit of our bar we have first to convert seconds to milliseconds (s * 1000), then divide between the resolution of the bar (s * 1000 to 1080). So our program is as follows:
The first cycle is only for aesthetics, to see how far the bar will fill. The second is that prints the bar with their respective breaks.
0 comments:
Post a Comment