Program skipping part of loop

I was working on this program for my class assignment and a guy in my class suggested using a nested for loop to improve my code though he didn't explain how to go about doing that or how it improves it. I ended up getting a 2nd year who does peer tutoring to assist me with trying to figure out how to do it. Well it turned out the guy in my first year class was either insane or on a programming level the 2nd year wasn't on and knew stuff he didn't even know existed. Anyways he tried to integrate it into my program but couldn't figure out how to do it. And now part of my program doesn't work and I can't figure out what the issue is. The issue seems to be that it skips the entire "while (errorCount < 6 || correctCount < 5)" loop or at least it appears to and then starts the "while (errorCount == 6 || correctCount == 5)" loop even though it doesn't meet the requirements. I've tried redoing the loop 4 times using various loops but it isn't working for me for some weird reason. So can someone help solve this?
And please keep in mind I know the coding is terrible my prof just teaches the bare basics and then leaves everything else to us to learn so I'm rush learning this and only have 6 weeks of experience.

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
#include <iostream>
#include <string>

using namespace std;
const string HIDDEN_WORD = "B A S I C S";

int main()
{
	//Declares Variables
	bool willYouPlay;
	int errorCount = 0, correctCount = 0;
	char playerInput = 0, yesOrNo = 0, head = '0', body = '|', leftArm = '//', righArm = '\\', leftLeg = '//', rightLeg = '\\';
	string gallows1 = "     |------|--";
	string gallows2 = "     |         "; //12 head placement
	string gallows3 = "     |         "; //11,12,13 left arm, body, and right arm placement
	string gallows4 = "     |         "; //11,13 left leg and right leg placement
	string gallows5 = "     |         ";
	string gallows6 = "  =======      ";
	string letterPlaces = "_ _ _ _ _ _";
	string alphabet = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
	string blockOut = "_"; //Blocks correctly guessed letters in alphabet

	willYouPlay = false;

	while (!willYouPlay)
	{
		//Displays Gallows
		cout << gallows1 << endl;
		cout << gallows2 << endl;
		cout << gallows3 << endl;
		cout << gallows4 << endl;
		cout << gallows5 << endl;
		cout << gallows6 << endl << endl;
		cout << letterPlaces << endl << endl;
		cout << alphabet << endl << endl;

		//Prompts user for and takes in input
		cout << "Please enter a letter >> ";
		cin >> playerInput;
		cout << endl;
		system("cls"); //Clears screen

		//Decides if inputted letter is part of the word BASICS or not
//***************************************************************************
		while (errorCount < 6 || correctCount < 5)
		{
			if (playerInput == 'B' || playerInput == 'b' || playerInput == 'A' || playerInput == 'a' || playerInput == 'S' || playerInput == 's' || playerInput == 'I' || playerInput == 'i' || playerInput == 'C' || playerInput == 'c')
			{
				correctCount++;
				if (playerInput == 'B' || playerInput == 'b')
				{
					letterPlaces[0] = HIDDEN_WORD[0];
					alphabet[2] = blockOut[0];
				}
				else if (playerInput == 'A' || playerInput == 'a')
				{
					letterPlaces[2] = HIDDEN_WORD[2];
					alphabet[0] = blockOut[0];
				}
				else if (playerInput == 'S' || playerInput == 's')
				{
					letterPlaces[4] = HIDDEN_WORD[4];
					letterPlaces[10] = HIDDEN_WORD[10];
					alphabet[36] = blockOut[0];
				}
				else if (playerInput == 'I' || playerInput == 'i')
				{
					letterPlaces[6] = HIDDEN_WORD[6];
					alphabet[16] = blockOut[0];
				}
				else if (playerInput == 'C' || playerInput == 'c')
				{
					letterPlaces[8] = HIDDEN_WORD[8];
					alphabet[4] = blockOut[0];
				}
				if (correctCount == 5)
				{
					break;
				}
			}
			else// if (playerInput != 'B' || playerInput != 'b' || playerInput != 'A' || playerInput != 'a' || playerInput != 'S' || playerInput != 's' || playerInput != 'I' || playerInput != 'i' || playerInput != 'C' || playerInput != 'c')
			{
				errorCount++;
				if (errorCount == 1)
				{
					gallows2[12] = head;
				}
				else if (errorCount == 2)
				{
					gallows3[12] = body;
				}
				else if (errorCount == 3)
				{
					gallows3[11] = leftArm;
				}
				else if (errorCount == 4)
				{
					gallows3[13] = righArm;
				}
				else if (errorCount == 5)
				{
					gallows4[11] = leftLeg;
				}
				else if (errorCount == 6)
				{
					gallows4[13] = rightLeg;
					break;
				}
			}
		 }
//***************************************************************************
		while (errorCount == 6 || correctCount == 5)
		{
			if (correctCount == 5)
			{
				cout << "CONGRATULATIONS FOR WINNING!!!" << endl << endl;
				cout << "Press Y to play again or press N find out what you won!!!" << endl;
				cin >> yesOrNo;

				if (yesOrNo == 'Y' || yesOrNo == 'y')
				{
					system("cls"); //Clears screen
					//reset's changed variables to initial values
					errorCount = 0;
					correctCount = 0;
					playerInput = 0;
					yesOrNo = 0;
					gallows2 = "     |         ";
					gallows3 = "     |         ";
					gallows4 = "     |         ";
					letterPlaces = "_ _ _ _ _ _";
					alphabet = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
					willYouPlay = false; //Restarts program
				}
				else// if (yesOrNo != 'Y' || yesOrNo != 'y')
				{
					system("cls"); //Clears screen
					cout << "YOU'VE WON THE FABULOUS PRIZE OF ABSOLUTLY NOTHING!!! If you count your life as nothing that is." << endl << endl;
					cout << "Programmed by: _____" << endl << endl;
					willYouPlay = true; //Ends program
				}
			}
			else if (errorCount == 6)
			{
				cout << gallows1 << endl;
				cout << gallows2 << endl;
				cout << gallows3 << endl;
				cout << gallows4 << endl;
				cout << gallows5 << endl;
				cout << gallows6 << endl << endl;
				cout << "Well that's a sad turn of events... what do you say care for another go?" << endl << endl;
				cout << "Y|N" << endl;
				cin >> yesOrNo;

				if (yesOrNo == 'Y' || yesOrNo == 'y')
				{
					system("cls"); //Clears screen
					//reset's changed variables to initial values
					errorCount = 0;
					correctCount = 0;
					playerInput = 0;
					yesOrNo = 0;
					gallows2 = "     |         ";
					gallows3 = "     |         ";
					gallows4 = "     |         ";
					letterPlaces = "_ _ _ _ _ _";
					alphabet = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
					willYouPlay = false; //Restarts program
				}
				else// if (yesOrNo != 'Y' || yesOrNo != 'y')
				{
					system("cls"); //Clears screen
					cout << "Well that's rather unfortunate... but everything has an end I suppose... well... maybe it's just a new beginning..." << endl << endl;
					cout << "Programmed by: _____" << endl << endl;
					willYouPlay = true; //Ends program
				}
			}
		}
	}
	system("pause");
	return 0;
}
I think you should not implicitly criticise your teacher (the "I don't understand, therefore it is the teacher's fault" syndrome), nor knock your class colleague, who made an eminently sensible suggestion. "Nested" loops just mean one loop fully inside another - their use here would (a) make the code more readable and (b) make it more flexible than simply guessing one particular word "BASICS" - there's not much point in playing this game twice. Some string or character functions might also pre-empt the need for nested loops.

In terms of your loop skipping ... your prompt for playerInput is only done once and then you keep running the following "while" loop with the SAME value; you need to move this request for playerInput to immediately after your current line 46.

This might just about get the game to run, but it won't be correct because I could, for example, put the letter 'A' in 5 times and, because it was just one of the correct letters, it will accumulate my correctCount. You need a check that this letter hasn't already been chosen (e.g. by checking the current state in alphabet).

There are a huge number of problems with this program and I doubt that either myself or anyone else will want to spend much time on it unless you make it more readable (as your classmate suggested). Here are just a few suggestions:

- most compilers will need #include <cstdlib> in order to use system() - add this;
- fix the constants that are set incorrectly; leftArm and leftLeg are NOT using escape characters: think about the difference between / and \
- don't assign integers to character variables (playerInput, yesOrNo) - they don't need initialising anyway
- whilst debugging, remove anything to do with drawing the gallows - it just makes your program unreadable; you can put this back once all the rest of the logic is correct; you only need the accumulated error and correct counts whilst you are getting the program logic correct;
- whilst debugging, please don't clear the screen - you need to see what is going on
- whilst debugging, print out some intermediate variables (especially errorCount and correctCount)
- don't put spaces in HIDDEN_WORD or alphabet - it makes it really difficult to count positions;
- consider using toupper() to avoid having to consider both 'b' and 'B' for example


Make the first suggested change (moving your playerInput prompt into the required while loop) and fix any compiler errors/warnings. I'm afraid I (and probably most) cannot afford the time to advise any further on this until you make this program vastly more readable. It also tends to put me off if you imply that your teacher or classmates are unhelpful.
Line 12: Truncation of character constant left_arm and left_leg. '//' is two bytes. You're trying to store that into a single character.

As previously pointed out, your game is coded to work with a single hidden word. If you change the hidden word, you have recode the program. Not a good design.

The sense of willYouPlay seems backwards.

Lose the spaces in HIDDEN_WORD and alphabet.

You draw the gallows in multiple places. Consider doing this in once place. i.e. a function.

Ditto for resetting the game variables.
Topic archived. No new replies allowed.