how do i make a program loop?

How do i get this program to loop, also how to make the text green? Thanks.

/*Halloween program(not finished)


*/

#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<stdlib.h>
#include<windows.h>

using namespace std;

int main()
{
//declare variables
char type = ' ';
char list = ' ';

//enter input items

cout << "List of pictures(Y or N): ";
cin >> list;

if (list != 'Y' && list != 'N')
cout << "Please enter Y(es) or N(o). ";
else
{
if (list == 'Y')
cout << "w(itch)" << endl << "p(umpkin)" << endl << "g(host)" << endl;

}

cout << "Type(in all lowercase): ";
cin >> type;


//display

if (type != 'w' && type != 'p' && type != 'g')
cout << "Please enter witch, pumpkin or ghost" << endl;
else
{
switch (type)
{
case 'w':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << " ( '' ) " << endl;
cout << " ( _ * " << endl;
cout << " * ( / \ ___ " << endl;
cout << " " " _/ / " << endl;
cout << " ( * ) ___/ | " << endl;
cout << " ) '' _ o)'-./__ " << endl;
cout << " * _ ) (_, . $$$ " << endl;
cout << " ( ) __ __ 7_ $$$$ " << endl;
cout << " ( : { _) '--- $\ " << endl;
cout << " ______'___//__\ ____, \ " << endl;
cout << " ) ( \_/ _____\_ " << endl;
cout << " .' \ \------''. " << endl;
cout << " |=' '=| | ) " << endl;
cout << " | | | . _/ " << endl;
cout << " \ (. ) , / /__I_____\ " << endl;
cout << " '._/_)_(\__.' (__,(__,_] " << endl;
cout << " @---()_.'---@ " << endl;
break;
case 'p':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << " ,---._" << endl;
cout << " ,~~,-._ `._ " << endl;
cout << " v'~ `-. `. " << endl;
cout << " _,- ~. \ " << endl;
cout << " .' ,--`. `\_ " << endl;
cout << " V-'~ ,'~~~`-. `-. " << endl;
cout << " ___ /_/~~~) ` `. `._ " << endl;
cout << " ____,---' ; V `.__ `--, `; " << endl;
cout << " `-._ ; `. ____) : " << endl;
cout << " `.; ; .' ,' _ | " << endl;
cout << " ; | ; ,';'~~~`--' `; : " << endl;
cout << " ,': .-' `,' __ __ : | " << endl;
cout << " )_ `-, ___ __ (__:__) ; ; " << endl;
cout << " _,---. \___,'` ~`---; `-. ||| ; ; " << endl;
cout << " _,-/ :; ! ` `| `-. |~~~~| ; : " << endl;
cout << " _,-' /~ .,' ; !! `.. ``. `. : ; | : " << endl;
cout << " ,' ,-' .; `; !! _,'-' ,--._ ====\__/===: `. " << endl;
cout << " .' ,-' ,--. ~~`-. !! ~ ,' ` `./ \ | | " << endl;
cout << " .' :; ,' \ !: . ;--.__ `;. |. ~.| : ; " << endl;
cout << " .' ,; ' ,-'~~`-. ,! ;-' #; `;. \____/ : `. " << endl;
cout << " .' ,; /__ `-._,'!!( _(0'~~`-' `;. `. ; ; " << endl;
cout << " .' ,; ,' `---._(0)) !! ~ _,-, `; `. ; : " << endl;
cout << " ; ,; ,' ;;-.__,-._~~~ !!__,--::::|. `;: : `; ) " << endl;
cout << " ; :| ,' ;/;;; :::;;;;----'|:: |::;\/#. `; | ) ; " << endl;
cout << " : |: ,' ,' :/ :;; \/))):;;::/ ::' ##: ;; ; ; | " << endl;
cout << " | :| :; : `' \/ \/ `' `' ##; ; .' ,' ; " << endl;
cout << " : |; || .' ;\. ____ __,--._ ##; ;' .'--' ; " << endl;
cout << " _,--`. `. :: `./; /\/;:;,-' `-. `--.__ .'~ ,'~ " << endl;
cout << " / ;. `; ``. :::;\;.-'~~`./~~\/\ .. _ ::: --. ' ,-'~ " << endl;
cout << " / . `. `; ` ~~~ ;~ ~~~~~~`--.__~~`-. ::: ) ~ " << endl;
cout << "/' ;`--`. `. `. :; `; ; `---`._ ,' " << endl;
cout << "`. \/ `-.` `_,_ `:,-'-. `. :_,_ ; `--' " << endl;
cout << " `. `. ` (___)-: ( ) :--,-'- -(___),'~~~`. " << endl;
cout << " {_ `. `.___.' ( ; ;) :((:)): " << endl;
cout << " `. ` `--' `.___.' " << endl;
cout << " `. `. ;;:::; " << endl;
cout << " `- `. ;;;. .; " << endl;
cout << " `-. `.__ \\;;; - ;; // " << endl;
cout << " `. ` `--..___ \\,--v-, // " << endl;
cout << " `--._ ~~~~~`)____(// " << endl;
cout << " ) ~~ ~~~~~~; " << endl;
cout << " `. ~~ ------; " << endl;
cout << " `.~~_ ______,' " << endl;
cout << " `. `.--';: |: " << endl;
cout << " ; `. Cc; Cc " << endl;
cout << " `. ; __ " << endl;
cout << " `. `-' ~\ " << endl;
cout << " `-.__,--'~ " << endl;
break;
case 'g':
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
cout << " .---. " << endl;
cout << " / \ " << endl;
cout << "---- _ | O O | _ --- " << endl;
cout << "\ ~-. `. .' .-~ / " << endl;
cout << " \ ~-_> <_-~ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ / " << endl;
cout << " \ \ " << endl;
cout << " \ \ " << endl;
cout << " \ \ " << endl;
cout << " \ \ " << endl;
cout << " \ l " << endl;
cout << " \ / " << endl;
cout << " V " << endl;
}


system("pause");
return 0;


}
}
Last edited on
Hello squiffy123,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

I would take lines 23 - 138 and put them in a do/while or while loop.

As far as the use of "SetConsoleTextAttribute"function it takes more than just this line to change the text or background color.

I will have to find the function I use and I will post it for you.

Hope that helps,

Andy

P.S. I deleted some blank lines, so the line numbers I used may be a bit off. Sorry
Last edited on
Hello squiffy123,

As I promised here is the code to set the foreground and background colours. I also reworked the code a bit to show you what can be done. Compare what I did to what you have.

I left the looping for you to work on. It is not that hard and this might help http://www.cplusplus.com/doc/tutorial/control/#dowhile Although a while loop will work I am thinking the do/while loop because it will always execute one time.

I have to put the code in the next message because it makes this one to long.

The first thing you should notice is the header file includes. "algorithm" is not needed or used in the program. The other I have comments as to why they are there.

Next it would be better if you could learn not to use the line using namespace std; it WILL get you in trouble some day.

Following that are the two prototypes I added for the two functions that follow "main".

The last lines just before "return" I use with my VS IDE to keep the console window open. It is a replacement of sorts for "system("pause")". It is best not to use "system" anything as it will leave the program vulnerable.

Following "main" is the function I use with windows to set the foreground and background colours. The lace function is one I found to clear the screen.

Be advised these two functions standard C++ functions and only work on the Windows operating system. They will not work on any flavor of Unix, Mac or any other operating system as they do things differently.

Hope that helps,

Andy
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*Halloween program(not finished)


*/

#include<iostream>
#include<iomanip>
#include<string>
#include <cctype>  // <--- Added for "std::tolower()" and "std::toupper()".
//#include<algorithm>
#include<stdlib.h>
#include<windows.h>
#undef max  // <--- This is need when windows is included and must be done after "windows.h" and before you incllude "limits".
#include <limits>  // <--- Added.

#define CLS cls()  // <--- In the program the capital letters will be replaced with the function call. You add the semi-colon.

using namespace std;

void SetColor(int forground, int background);
void cls();

int main()
{
	//declare variables
	char type{};  // <--- Changed. The empty {}s do the same thing.
	char choice{};  // <--- Changed since "list" is nolonger used. Used in part of the program not here.

	//enter input items

	//cout << "List of pictures(Y or N): ";
	//cin >> list;
	//list = std::toupper(list);

	//if (list != 'Y' && list != 'N')
	//	cout << "Please enter Y(es) or N(o). ";
	//else
	//{
	//	if (list == 'Y')

		CLS;

		cout << "\n (w)itch" << endl << " (p)umpkin" << endl << " (g)host" << endl;  // <--- Changed.

		//}

		cout << " Enter choice: ";  // <--- Changed.
		cin >> type;
		type = std::tolower(type);  // <--- Added. Makes sure what is entered ends up a lower case letter.

		//display

		if (type != 'w' && type != 'p' && type != 'g')
			cout << "Please enter (w)itch, (p)umpkin or (g)host" << endl;  // <--- Changed.
		else
		{
			CLS;
			std::cout << "\n\n";

			switch (type)
			{
			case 'w':
				SetColor(10, 0);  // <--- Parameters are forground and background. Green on black.
				cout << " ( '' ) " << endl;
				cout << " ( _ * " << endl;
				cout << " * ( / \ ___ " << endl;
				cout << " " " _/ / " << endl;
				cout << " ( * ) ___/ | " << endl;
				cout << " ) '' _ o)'-./__ " << endl;
				cout << " * _ ) (_, . $$$ " << endl;
				cout << " ( ) __ __ 7_ $$$$ " << endl;
				cout << " ( : { _) '--- $\ " << endl;
				cout << " ______'___//__\ ____, \ " << endl;
				cout << " ) ( \_/ _____\_ " << endl;
				cout << " .' \ \------''. " << endl;
				cout << " |=' '=| | ) " << endl;
				cout << " | | | . _/ " << endl;
				cout << " \ (. ) , / /__I_____\ " << endl;
				cout << " '._/_)_(\__.' (__,(__,_] " << endl;
				cout << " @---()_.'---@ " << endl;
				break;
			case 'p':
				SetColor(10, 0);
				cout << " ,---._" << endl;
				cout << " ,~~,-._ `._ " << endl;
				cout << " v'~ `-. `. " << endl;
				cout << " _,- ~. \ " << endl;
				cout << " .' ,--`. `\_ " << endl;
				cout << " V-'~ ,'~~~`-. `-. " << endl;
				cout << " ___ /_/~~~) ` `. `._ " << endl;
				cout << " ____,---' ; V `.__ `--, `; " << endl;
				cout << " `-._ ; `. ____) : " << endl;
				cout << " `.; ; .' ,' _ | " << endl;
				cout << " ; | ; ,';'~~~`--' `; : " << endl;
				cout << " ,': .-' `,' __ __ : | " << endl;
				cout << " )_ `-, ___ __ (__:__) ; ; " << endl;
				cout << " _,---. \___,'` ~`---; `-. ||| ; ; " << endl;
				cout << " _,-/ :; ! ` `| `-. |~~~~| ; : " << endl;
				cout << " _,-' /~ .,' ; !! `.. ``. `. : ; | : " << endl;
				cout << " ,' ,-' .; `; !! _,'-' ,--._ ====\__/===: `. " << endl;
				cout << " .' ,-' ,--. ~~`-. !! ~ ,' ` `./ \ | | " << endl;
				cout << " .' :; ,' \ !: . ;--.__ `;. |. ~.| : ; " << endl;
				cout << " .' ,; ' ,-'~~`-. ,! ;-' #; `;. \____/ : `. " << endl;
				cout << " .' ,; /__ `-._,'!!( _(0'~~`-' `;. `. ; ; " << endl;
				cout << " .' ,; ,' `---._(0)) !! ~ _,-, `; `. ; : " << endl;
				cout << " ; ,; ,' ;;-.__,-._~~~ !!__,--::::|. `;: : `; ) " << endl;
				cout << " ; :| ,' ;/;;; :::;;;;----'|:: |::;\/#. `; | ) ; " << endl;
				cout << " : |: ,' ,' :/ :;; \/))):;;::/ ::' ##: ;; ; ; | " << endl;
				cout << " | :| :; : `' \/ \/ `' `' ##; ; .' ,' ; " << endl;
				cout << " : |; || .' ;\. ____ __,--._ ##; ;' .'--' ; " << endl;
				cout << " _,--`. `. :: `./; /\/;:;,-' `-. `--.__ .'~ ,'~ " << endl;
				cout << " / ;. `; ``. :::;\;.-'~~`./~~\/\ .. _ ::: --. ' ,-'~ " << endl;
				cout << " / . `. `; ` ~~~ ;~ ~~~~~~`--.__~~`-. ::: ) ~ " << endl;
				cout << "/' ;`--`. `. `. :; `; ; `---`._ ,' " << endl;
				cout << "`. \/ `-.` `_,_ `:,-'-. `. :_,_ ; `--' " << endl;
				cout << " `. `. ` (___)-: ( ) :--,-'- -(___),'~~~`. " << endl;
				cout << " {_ `. `.___.' ( ; ;) :((:)): " << endl;
				cout << " `. ` `--' `.___.' " << endl;
				cout << " `. `. ;;:::; " << endl;
				cout << " `- `. ;;;. .; " << endl;
				cout << " `-. `.__ \\;;; - ;; // " << endl;
				cout << " `. ` `--..___ \\,--v-, // " << endl;
				cout << " `--._ ~~~~~`)____(// " << endl;
				cout << " ) ~~ ~~~~~~; " << endl;
				cout << " `. ~~ ------; " << endl;
				cout << " `.~~_ ______,' " << endl;
				cout << " `. `.--';: |: " << endl;
				cout << " ; `. Cc; Cc " << endl;
				cout << " `. ; __ " << endl;
				cout << " `. `-' ~\ " << endl;
				cout << " `-.__,--'~ " << endl;
				break;
			case 'g':
				SetColor(10, 0);
				cout << " .---. " << endl;
				cout << " / \ " << endl;
				cout << "---- _ | O O | _ --- " << endl;
				cout << "\ ~-. `. .' .-~ / " << endl;
				cout << " \ ~-_> <_-~ / " << endl;
				cout << " \ / " << endl;
				cout << " \ / " << endl;
				cout << " \ / " << endl;
				cout << " \ / " << endl;
				cout << " \ / " << endl;
				cout << " \ / " << endl;
				cout << " \ \ " << endl;
				cout << " \ \ " << endl;
				cout << " \ \ " << endl;
				cout << " \ \ " << endl;
				cout << " \ l " << endl;
				cout << " \ / " << endl;
				cout << " V " << endl;
			}

			SetColor(7, 0);  // <--- White on black.


	// <--- Better to use than "system("pause");"
	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needid. If you have to press enter to see the prompt it is not needed.
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	//system("pause");
	return 0;
}

void SetColor(int forground, int background)
{
	WORD consoleColor;
	HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_SCREEN_BUFFER_INFO csbi;

	if (GetConsoleScreenBufferInfo(hStdOut, &csbi))
	{
		consoleColor = (forground + (background * 16));
		SetConsoleTextAttribute(hStdOut, consoleColor);
	}
}  //  End SetColor()

// <--- This goes with the "cls" function and needs to be here before the function.
#define PERR(bSuccess, api){if(!(bSuccess)) printf("%s:Error %d from %s on line %d\n", \
__FILE__, GetLastError(), api, __LINE__);}

void cls()
{
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coordScreen = { 0, 0 };    /* here's where we'll home the
									 cursor */
	BOOL bSuccess;
	DWORD cCharsWritten;
	CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
	DWORD dwConSize;                 /* number of character cells in
									 the current buffer */

									 /* get the number of character cells in the current buffer */

	bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
	PERR(bSuccess, "GetConsoleScreenBufferInfo");
	dwConSize = csbi.dwSize.X * csbi.dwSize.Y;

	/* fill the entire screen with blanks */

	bSuccess = FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
		dwConSize, coordScreen, &cCharsWritten);
	PERR(bSuccess, "FillConsoleOutputCharacter");

	/* get the current text attribute */

	bSuccess = GetConsoleScreenBufferInfo(hConsole, &csbi);
	PERR(bSuccess, "ConsoleScreenBufferInfo");

	/* now set the buffer's attributes accordingly */

	bSuccess = FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
		dwConSize, coordScreen, &cCharsWritten);
	PERR(bSuccess, "FillConsoleOutputAttribute");

	/* put the cursor at (0, 0) */

	bSuccess = SetConsoleCursorPosition(hConsole, coordScreen);
	PERR(bSuccess, "SetConsoleCursorPosition");
	return;
}  //  End CLS

I did notice one thing. The pumpkin is bigger than my 27" screen the way I have it set up.
Topic archived. No new replies allowed.