Loading Box in C++

Here I've created a code in which a loading screen will come.
Can anyone give me suggestion how to make it more attractive
Here is code

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<Windows.h>

using namespace std;

void print(int b, char a);

int main()
{
	system("cls");
	char a = 178;
	int z;
	float l = 1.5;
	for (int i = 0; i<75; ++i)
	{
		system("cls");

		z = 1 + rand() % (30 + 1 - 1) + 30;
		cout << "\n\n\n\n\n\n\n\n\t\t\t\tLoading " << (int)l << " %\n\n";
		cout << (char)201;
		print(76, 205);
		cout << (char)187 << endl << (char)186 << ' ';
		print(i, a);
		print(75 - i, ' ');
		cout << (char)186 << endl;

		//second line
		cout << (char)186 << ' ';
		print(i, a);
		print(75 - i, ' ');
		cout << (char)186 << endl;
		//second line

		//third line
		cout << (char)186 << ' ';
		print(i, a);
		print(75 - i, ' ');
		cout << (char)186 << endl;
		//third line

		//forth line
		cout << (char)186 << ' ';
		print(i, a);
		print(75 - i, ' ');
		cout << (char)186 << endl;
		//forth line

		cout << (char)200;
		print(76, 205);
		cout << (char)188;
		cout << "\n\n\t\tTip : After loading press any key to continue.";
		l = l + 1.33333333;
		Sleep(z);
	}

	_getch();
	return 0;
}

void print(int b, char a = ' ')
{
	for (int uuu = 0; uuu<b; ++uuu)
		cout << a;
}
char a = 178; — undefined behavior. If your platform uses signed representation of char, then overflow happens. On my machine result looks like that: http://imgur.com/jRIc6pC

Never use chars to store integral values. Use explicit signed or unsigned chars. In your case use unsigned chars.

Do not update screen so often. It creates terrible flickering. Use crossplatform solutions instead of windows ones (get rid of Windows.h). Stop using nonstandard outdated crap, which is not supported in modern compilers anymore (dos.h and dreaded conio.h), use C++ headers instead of C ones (cstdlib instead of stdlib.h)
Last edited on
Use crossplatform solutions instead of windows ones (get rid of Windows.h). Stop using nonstandard outdated crap, which is not supported in modern compilers anymore (dos.h and dreaded conio.h), use C++ headers instead of C ones (cstdlib instead of stdlib.h)




Oh really tell me one crossplatform solution.There is none!!!!
Oh really tell me one crossplatform solution.There is none!!!!
1
2
std::this_thread::sleep_for(100ms);
//well, ms literal will be avaliable only in C++14, but you can use chrono::duration instead 
But in my PC it looks like
http://i.imgur.com/htjby3k.png
That is why it is called "undefinded behavior"! Everything can happen. It can work perfectly, it can work with errors, it can crash, it can fry your PC, it can steal your CC information or it can start armageddon. Standard allows any of these possibilities.
That is why you should avoid UB: if it works for you now, it does not means that it will work for others or even for you tomorrow.

If you change character tupe to unsigned char. your program will become well-formed and predictable.
Last edited on
Topic archived. No new replies allowed.