dynamic loading bar

Jul 6, 2020 at 6:53pm
Hey lads,

I have to write a code in order to get a dynamic loading bar,( ex. windows is loading files). -

https://www.partitionwizard.com/images/uploads/articles/2019/07/windows-stuck-at-loading-files/windows-stuck-at-loading-files-thumbnail.jpg

I have done smthg, but it will take to many code lines in order to complete it, so I am thinking how to make it shorter with a loop or goto function maybe, can you help pls.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int main()
{
 x = _getch();    
        
    cout<< "\n\n\n\n\n\n\n\t\t\t\t\t\t\tWindows will start to load shortly... "<<endl;
    Sleep(4000);
     system("cls");
     
     cout<< " \n\n\n\n\n\n\n\t\t\t\t\t\t\t\t\t...Please wait..."<<endl;
     Sleep(3000);
     system("cls");
     cout<<" \n\n\n\n\n\n\n\t\t\t\t\t\t\t\tWindows is loading files \n\t";
     

   cout<<(char)178<<(char)178;
    Sleep(2500);
   cout<<(char)178<<(char)178;
    Sleep(3000);
   cout<<(char)178<<(char)178;
    Sleep(1000);
   cout<<(char)178<<(char)178;
    Sleep(4000);
   cout<<(char)178<<(char)178; 
    Sleep(500);
   cout<<(char)178<<(char)178;    
    Sleep(500);
   cout<<(char)178<<(char)178;
    Sleep(500);
   cout<<(char)178<<(char)178;
     cout<<(char)178<<(char)178;
    Sleep(2500);
   cout<<(char)178<<(char)178;
    Sleep(3000);
   cout<<(char)178<<(char)178;
    Sleep(1000);
   cout<<(char)178<<(char)178;

 
      return 0;
  }
Jul 6, 2020 at 7:10pm
I got nothing on what you wrote.
typically a progress bar divides the amount of work being done (eg, % of a file read in bytes by size, or # of lines, etc) and 'draws' (can be ascii art) the bar filled to that percent until done. Some text codes just display the numerical % instead. Since you use getch, you may also use gotoxy to over-write so the bar changes in place rather than redrawn line by line in the console.

so say your progress bar is drawn with 20 '-' characters, maybe '_' or spaces for the empty part.
that is 5% per tick. So if you were processing a file with 300 lines... every 15th line you add a tick to your bar, because 15*1, 15*2 (30)... 15*20 = 300. If it does not work out evenly, you can fudge the last tick a little and just wait for true 100% to fill it in, or something like that.

so basically, yes a loop... loop over the thing you are processing, and every so many loop iterations, you update your progress bar.
something like

1
2
3
4
5
6
7
mypbar.init(somevalue); //some sort of setup to tell it what 100% means. 
for(int i = 0; i < somevalue; i++)
{
      if(i%something == 0) mypbar.update(i); //or however you have your progress
// bars defined here.  I assume long term, an object with a method.
}
mybar.update(somevalue); //you may need one more call to have it hit 100% here 

Last edited on Jul 6, 2020 at 7:13pm
Jul 6, 2020 at 7:11pm
Don't use goto, use real loops almost in every case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   cout<<(char)178<<(char)178;
    Sleep(2500);
   cout<<(char)178<<(char)178;
    Sleep(3000);
   cout<<(char)178<<(char)178;
    Sleep(1000);
   cout<<(char)178<<(char)178;
    Sleep(4000);
   cout<<(char)178<<(char)178; 
    Sleep(500);
   cout<<(char)178<<(char)178;    
    Sleep(500);
   cout<<(char)178<<(char)178;
    Sleep(500);
   cout<<(char)178<<(char)178;

Note that you're repeating the same thing here at least 8 or 12 times. But the thing that's changing is the time you sleep for. So you could factor out the different sleep times into an array or some other function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <Windows.h>
using std::cout;

int main()
{
    int sleep_times[] = { 2500, 3000, 100,  400,  500,
                          500,   500,   0, 2500, 3000,
                          100  };
    
    for (int i = 0; i < 11; i++)
    {
        cout << (char)178 << (char)178;
        Sleep(sleep_times[i]);
    }
    cout << (char)178 << (char)178;
 
    return 0;
}
Jul 6, 2020 at 7:16pm
Okay. So make a loop. (Don't ever use goto, though. It is not allowed for beginners.)
You could also make a char variable instead of saying (char)178 all the time:

 
char c = (char)178;

Jul 6, 2020 at 7:20pm
That reminds me, I forgot to mention that technically (char)178 is undefined behavior if char is signed (and 8-bit) on the given platform.

I think Windows will still print the same character if you do unsigned char c = 178 instead.

Edit: I might be over-complicating this, see below.
Last edited on Jul 6, 2020 at 7:58pm
Jul 6, 2020 at 7:30pm
technically (char)178 is undefined behavior

Are you sure about that?
I think (char)178 will simply reinterpret the bits, so no problem.
Still, it does smell like UB.
Jul 6, 2020 at 7:55pm
Yep I think I'm mistaken, according to what I read from https://en.cppreference.com/w/cpp/language/implicit_conversion --> Integral conversions,
If the destination type is signed, the value does not change if the source integer can be represented in the destination type. Otherwise [the result is implementation-defined (until C++20)] [the unique value of the destination type equal to the source value modulo 2^n where n is the number of bits used to represent the destination type. (since C++20)]. (Note that this is different from signed integer arithmetic overflow, which is undefined).


So while it technically could still be bad in C++17, it probably isn't on any sane platform.
Last edited on Jul 6, 2020 at 7:57pm
Jul 6, 2020 at 8:09pm
I'm mistaken too since I assumed it was well-defined all along.
As you say, it's probably the usual implementation anyway.
That's probably why C++20 enshrined the practice.
Jul 7, 2020 at 2:02am
To get back to where @jonnin left off after a wander into the wilderness of char 178 esoterica this might be of some use unless all the sleeps are just to make a simulation rather than the real thing.

The second sample and test appears to be adaptable to the OP problem and it actually runs which is a bonus.

https://codereview.stackexchange.com/questions/186535/progress-bar-in-c
Jul 7, 2020 at 11:01am
thank you all for patience and explanation, truly appreciate it
Topic archived. No new replies allowed.