Header file release: credits.hpp

Hello. I made this code and randomly put it here because I'm bored.
Here's the 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
	Code by : Alexander123
*/


#ifndef CREDITS_H
#define CREDITS_H

#include <include.h>

namespace credits
{
	void clean(string title, string dev)
	{
		if(title.size() == 9 and dev.size() == 9)
		{
			HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleTextAttribute(hStdOut, 0xF);
			cout << "______________________________\n";
			cout << "|        "<<title<<"           |\n";
			cout << "|                            |\n";
			cout << "|            by:             |\n";
			cout << "|         "<<dev<<"          |\n";
			cout << "+----------------------------+\n\n";
			return;
		}
		else
		{
			cout << "An error has occurred.\nThe TITLE must be 9\ndigits long.\n(Use spaces if needed.)\n";
		}
	}
	
	
	void red(string title, string dev)
	{
		if(title.size() == 9 and dev.size() == 9)
		{
			HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleTextAttribute(hStdOut, FOREGROUND_RED);
			cout << "______________________________\n";
			cout << "|        "<<title<<"           |\n";
			cout << "|                            |\n";
			cout << "|            by:             |\n";
			cout << "|         "<<dev<<"          |\n";
			cout << "+----------------------------+\n\n";
			return;
		}
		else
		{
			cout << "An error has occurred.\nThe title must be 9\ndigits long.\n(Use spaces if needed.)\n";
		}
	}
	
	
	void green(string title, string dev)
	{
		if(title.size() == 9 and dev.size() == 9)
		{
			HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN);
			cout << "______________________________\n";
			cout << "|        "<<title<<"           |\n";
			cout << "|                            |\n";
			cout << "|            by:             |\n";
			cout << "|         "<<dev<<"          |\n";
			cout << "+----------------------------+\n\n";
			return;
		}
		else
		{
			cout << "An error has occurred.\nThe title must be 9\ndigits long.\n(Use spaces if needed.)\n";
		}
	}
	
	
	void blue(string title, string dev)
	{
		if(title.size() == 9 and dev.size() == 9)
		{
			HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
			SetConsoleTextAttribute(hStdOut, 0x1F);
			cout << "______________________________\n";
			cout << "|        "<<title<<"           |\n";
			cout << "|                            |\n";
			cout << "|            by:             |\n";
			cout << "|         "<<dev<<"          |\n";
			cout << "+----------------------------+\n\n";
			return;
		}
		else
		{
			cout << "An error has occurred.\nThe title must be 9\ndigits long.\n(Use spaces if needed.)\n";
		}
	}
}

#endif 


I don't know why I made this...
Last edited on
So that we can critique it, of course :)

Notice that all of your <color> functions do almost exactly the same thing. This is the perfect opportunity to factor out the logic into a function with parameters.

Make a new function,
1
2
3
4
void show_credits(string title, string dev, int color)
{
    // ...
}

And have it be the same as, for example, your green function, but replace FOREGROUND_GREEN with color.

And then, your green function becomes:
1
2
3
4
5
6
7
8
9
void green(string TITLE, string DEV)
{
    show_credits(TITLE, DEV, FOREGROUND_GREEN);
}

void red(string TITLE, string DEV)
{
    show_credits(TITLE, DEV, FOREGROUND_RED);
}


Another improvement is to see if you generate the text that you print in a more dynamic way to allow for a range of title/dev lengths.

Also, think about how you name your functions. A function like "red()" or "green()" doesn't tell me what it does. What is the purpose of the function? Try to encode the purpose of the function in its name in some way, for example "show_credits" like I did.

PS: ALLCAPS names are usually reserved to be easily identified as macros (and sometimes enums or constants). You can have all-caps, non-macro names, but it goes against most style guides and common practices.
Last edited on
I'll try to make the code better.
Topic archived. No new replies allowed.