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
|
#include <iostream>
#include <windows.h> //colored couts go brrrr :)
using namespace std;
const HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//Prints in full color with (optional) automatic line breaks. Requires <windows.h> Returns nothing and takes up to 4 parameters:
//console (HANDLE): Used for colored text. Always set it to hConsole and don't worry about it.
//text (string): The text to be printed. Don't put a newline at the end.
//color (int): The color code of the text. Optional, defaults to white.
//linebreak (bool): Whether to end the line after printing. Optional, defaults to true.
void colorPrint(string text, int color = 15, bool linebreak = true, HANDLE console = hConsole)
{
if (linebreak) //Add a line break to the end of the text unless told not to
{
text += "\n";
}
SetConsoleTextAttribute(console, color); //Use black magic (native OS commands) to change the color of any text we print.
cout << text;
SetConsoleTextAttribute(console, 15); //Use more black magic to set the color back to white so that we don't start randomly printing other colors.
}
//Super messy and really just here because if you're reusing more than one line, you're doing something wrong.
int getWidthOrHeight(string d)
{
int input;
while (true)
{
cout << "Please enter the ";
colorPrint(d, 5, false);
cout << " of your kite in ";
colorPrint("cm", 5, false);
cout << ": ";
if (cin >> input) //This feels so wrong but it does in fact work.
{
break;
}
colorPrint("Oh no! It looks like something went wrong.\nPlease make sure your width is a number and try again.", 12);
cin.clear();
cin.ignore(80, '\n');
}
return input;
}
int main()
{
int width, height;
bool haveWidth = false, haveHeight = false; //If the user has entered a valid width and height yet. Used for looping.
colorPrint("Welcome to Kite Calculator!\n", 9);
colorPrint("Let's start with getting the dimensions of your kite!", 11);
width = getWidthOrHeight("width");
height = getWidthOrHeight("height");
}
|