help with a game

hello, a few days back i posted asking about the game i was programming, and now ive moved much farther along, and im having strange run-time errors, and problem recognizing strings from an array. the code is this:
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
#include <iostream>
#include <time.h>
#include <string>
#include "clearscreen.h"
int repeater(std::string answer);

int main()
{
	//Beginning of the game, defines everything important to functionality.
	srand((unsigned)time(NULL));
	const int NUM_CYCLES=4;
	const int NUM_COLORS=6;
	const std::string all_colors[NUM_COLORS] = {"red", "blue", "green", "yellow", "white", "black"};
	const std::string guess_number[NUM_CYCLES] = {"first", "second", "third", "last"};
	const std::string answers[2] = {"yes", "no"};

	std::string colors[NUM_CYCLES];
	std::string guesses[NUM_CYCLES];
	std::string answer;

	int counter, cycle, selection, right_color_place, right_color, wrong_color, second_cycle, game, last_cycle;
	game=1;

	//main loop of the game, allows for the game to be repeatable.
	while(game)
	{
	right_color_place=0;
	right_color=0;
	wrong_color=0;
	second_cycle=1;
	last_cycle=1;

	cycle=0;

	//code generator loop.
	while (cycle <=3)
	{
		for (counter=0;counter <=100; counter++)
		{
			selection=((rand() % 6));
		}

		colors[cycle] = all_colors[selection];

		std::cout << selection << std::endl << colors[cycle] << std::endl ;
		

		cycle++;
	}
	
	//where the game is actually played.
	while (second_cycle <= 12)
	{
		cycle=0;
		std::cout << "Colors in the right place: " << right_color_place;
		std::cout << " Colors in the wrong place: " << right_color;
		std::cout << " Wrong colors: " << wrong_color << std::endl;
		std::cout << "Current guess: " << second_cycle << std::endl;

		while(cycle <=3)
		{
			std::cout << "What's your " << guess_number[cycle] << " guess?\n";
			std::cin >> guesses[cycle];

			//this stupidly long if loop checks to make sure the player actually put in
			//one of the colors of the game.
			if ((guesses[cycle].compare(all_colors[1])==1) || (guesses[cycle].compare(all_colors[2])==1)
 || (guesses[cycle].compare(all_colors[3])==1) || (guesses[cycle].compare(all_colors[4])==1)
 || (guesses[cycle].compare(all_colors[5])==1) || (guesses[cycle].compare(all_colors[6])==1))
			{
				cycle++;
			}

			else
			{
				std::cout << "I'm sorry, but your guess didnt match one of the colors. \nPlease guess again." ;
			}
		}
		cycle=0;

		while(cycle <=3)
		{
			if (guesses[cycle].compare(colors[cycle])==0)
			{
				right_color_place++;
				cycle++;
			}

			//checks to see if the guess matches any of the selected colors.
			else if (guesses[cycle].compare(colors[0])||guesses[cycle].compare(colors[1])||guesses[cycle].compare(colors[2])||guesses[cycle].compare(colors[3]))
			{
				right_color++;
				cycle++;
			}

			else
			{
				wrong_color++;
				cycle++;
			}
			
		}

		//checks to see if the player has correctly guessed the code.
		if (right_color_place == NUM_CYCLES)
		{
			second_cycle=13;
		}

		else 
		{
			second_cycle++;
		}
	}

	//after game text akin to ending credits.
	while (second_cycle = 13)
	{
		if (right_color_place == NUM_CYCLES)
		{
			std::cout << "Congratulations! You won!\n";			
		}

		else
		{
			std::cout << "I'm sorry, you lost.\n The code was: " << colors[NUM_CYCLES] << "\n";
		}

		while (last_cycle==1)
		{
			std::cout << "Would you like to play again? (yes/no)\n";
			std::cin >> answer;
			
			//makes it so that the game can be played multiple times in a row.
			if (answer.compare(answers[1]) == 1)
			{
				game++;
				last_cycle++;
				clear_screen();
				second_cycle++;
			}

			else if (answer.compare(answers[2]) ==1)
			{
				game = 0;
				last_cycle++;
				clear_screen();
				second_cycle++;
			}

			else
			{
				std::cout << "I'm sorry that you can't type simple words. Please try again, and do better this time.";
				clear_screen();
				
			}
		}
	}
	}

	int temp;
	std::cout << "Enter a number to continue...";
	std::cin >> temp;
	return 0;
}


basically all the problems i'm having are involving .compare() when it gets to the part where it checks for comparison of multiple strings, it either crashes, or doesnt return the proper value.

im sorry if its muddled, but any help would be nice, thanks!
1. Arrays start at 0, not 1. Thus, the last element of all_colors is all_colors[5], not all_colors[6]. The same applies to your uses of the 'answers' array.
2. std::string::compare() returns 0, negative, or positive values. There's no guarantee that 1 will be the only positive value that your implementation returns, so checking for it is a mistake.
3. a.compare(b)==1 if a>b. Are you sure that's what you want to check?
4. std::string overloads the six relational operators. You might want to use that, instead.
thanks, the first part i already knew and was just throwing stuff at the wall in vexation.

2 & 3. i see what you mean there. am i right in assuming that a.compare(b)==0 if a==b?
after trying what ive gleamed here, i have everything working except for the part where it checks to see if a guessed color is wrong, it doesnt seem to recognize wrong colors and i dont know why.

Topic archived. No new replies allowed.