how to get out a loop after user enter the correct answer

Salam, i have a school project to submit by this week, basically my project is about crytographic games where the user input the letter to guess what are the real word that combine to become a sentences. I want to ask how to get out a loop after user enter the correct answer , well the maximum tries is five, i mean if user get the correct before using the five tries, it won't get out of the loop and the statement congratulations will not appear. How to make the statement congratulations appear after the user succesfully insert the correct letter.
here are my codes , as you can see, I have try many ways to make this thing happen but I had no more ideas on how to fix this.
please somebody help me
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  #include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
#include <string.h>
#include <stdio.h>
void Movie ();
void Idiom ();
void Country ();
void generator(char& teka, char answer[], char output[],bool& penentu,int& cubaan);
int penentuan(char output[],char answer[]);
using namespace std;
const int size = 30;
int main ()
{
	cout<<setw(55)<<"WELCOME TO CRYTOPGRAPHIC QUIZ GAMES!"<<endl;
	cout<<setw(73)<<"You must try to guess which alphabet represents the other alphabet.";
	cout<<endl<<endl;
    cout<<setw(45)<<"You are currently in"<<endl<<setw(38)<<"LEVEL 1"<<endl;

    const int MIN_VALUE = 1;
    const int MAX_VALUE = 3;

    int x;
    char option,again;

    unsigned seed = time(0);
    srand (seed);


    cout<<setw(58)<<"Your category have been choosen by computer :"<<endl;
    x = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;

    do
    {
        switch (x)
	{
		case 1: cout<<setw(38)<<"MOVIES"<<endl;
                Movie ();
		         break;
		case 2: cout<<setw(38)<<"IDIOMS"<<endl;
                Idiom ();
		         break;
		case 3: cout<<setw(38)<<"COUNTRY"<<endl;
                Country ();
		         break;
		default :;
	}
	cout<<endl<<setw(48)<<"Do you want to play again?"<<endl;
	cout<<setw(53)<<"Press Y if yes, press any key if no"<<endl;
	cout<<setw(35)<<" ";
	cin>>again;

    }while(again == 'Y'||again == 'y');

	return 0;
}
void Movie ()
{

	//system("COLOR D");


	char answer[size] = "THE SHAWSHANK REDEMPTION";
	char cipher[size] = "GSV HSZDHSZMP IVWVNKGRLM";
	char output[size] = "___ _________ __________";
	char tekaan; bool penentu = false;
	int TrueFalse, cubaan = 1;

	cout <<endl<<setw(53)<< "Guess the following cipher by letter" << endl <<setw(47)<< "You have maximum 5 tries" << endl;

	 do
    {
		//system("CLS");


		cout <<endl<<setw(47)<<"GSV HSZDHSZMP IVWVNKGRLM" << endl <<setw(47)<< "_ _ _ _ _ _ _ _ _ _ _" << endl;
		cout << endl;
		cout <<setw(47)<< output << endl <<endl;


		cout << "Your try - " << cubaan << " : ";
		cin >> tekaan;
		tekaan = toupper(tekaan);
		generator(tekaan, answer, output, penentu, cubaan);
		cout << endl;

        TrueFalse = penentuan(output, cipher);

        if(TrueFalse == 1)
        {
            break;
        }
        else
        {

        }


	}while(cubaan < 6);



	if (TrueFalse == 1)
	{

		cout << " CONGRATULATION ! YOU'VE GOT IT ALL CORRECT ! " << endl;
	}
	else
	{
		cout <<setw(45)<< " Sorry, you're WRONG! " << endl;
		cout <<setw(35)<< " The right answer : " << answer << endl;
	}
	//system("PAUSE");
}
void generator(char& teka, char answer[], char output[],bool& penentu,int& cubaan)
{

	for (int i = 0; i < size; i++)
	{

		if (teka == answer[i])
		{
			output[i] = teka;
			penentu = true;
		}

	}

	if (!penentu)
	{

		cubaan++;
	}
	else
	{
		penentu = false;
	}



}
int penentuan(char output[],char answer[])
{
    int TrueFalse;
	if (strcmp(output, answer) == 0)
	{
	    TrueFalse = 1;
		return TrueFalse;
	}
    else
    {
        TrueFalse = 0;
        return TrueFalse;
    }

}
void Idiom ()
{
	//system("COLOR D");


	char answer[size] = "CURIOSITY KILLS THE CAT";
	char cipher[size] = "XFIRLHRGB PROOH GSV XZG";
	char output[size] = "_________ _____ ___ ___";
	char tekaan; bool penentu = false;
	int cubaan = 1;

	cout <<endl<<setw(53)<< "Guess the following cipher by letter" << endl <<setw(47)<< "You have maximum 5 tries" << endl;

	while (cubaan < 6)
	{
		//system("CLS");

		cout <<endl<<setw(46)<< "XFIRLHRGB PROOH GSV XZG" << endl <<setw(58)<<"_ _ _ _ _ _ _ _ _  _ _ _ _ _  _ _ _  _ _ _ " << endl;
		cout << endl;
		cout <<setw(46)<< output << endl <<endl;


		cout << "try - " << cubaan << " : ";
		cin >> tekaan;
		tekaan = toupper(tekaan);
		generator(tekaan, answer, output, penentu, cubaan);
		cout << endl;



	}

	if (penentuan(output, cipher))
	{

		cout << " CONGRATULATION ! YOU'VE GOT IT ALL CORRECT ! " << endl;
	}
	else
	{
		cout <<setw(45)<< " Sorry, you're WRONG! " << endl;
		cout <<setw(33)<< " The right answer : " << answer << endl;
	}
	//system("PAUSE");
}
void Country ()
{
	//system("COLOR D");


	char answer[size] = "CROATIA";
	char cipher[size] = "ZYVIEWI";
	char output[size] = "_______";
	char tekaan; bool penentu = false;
	int cubaan = 1;

    cout <<endl<<setw(53)<< "Guess the following cipher by letter" << endl <<setw(47)<< "You have maximum 5 tries" << endl;

	while (cubaan < 6)
	{
		//system("CLS");


		cout <<setw(38)<< "ZYVIEWI" << endl <<setw(42)<< "_ _ _ _ _ _ _ " << endl;
		cout << endl;
		cout <<setw(38)<< output << endl <<endl;


		cout << "Your try - " << cubaan << " : ";
		cin >> tekaan;
		tekaan = toupper(tekaan);
		generator(tekaan, answer, output, penentu, cubaan);
		cout << endl;



	}

	if (penentuan(output, cipher))
	{

		cout << " CONGRATULATION ! YOU'VE GOT IT ALL CORRECT ! " << endl;
	}
	else
	{
		cout <<setw(45)<< " Sorry, you're WRONG! " << endl;
		cout <<setw(40)<< " The right answer : " << answer << endl;
	}
	//system("PAUSE");
}


Last edited on
I believe your problem is at

1
2
3
4
5
6
7
8
9
10
11
12
13
do
{
...
if(TrueFalse == 1)
{
      break;
}
else
{

}
...
}while(cubaan < 6);


To my knowledge the break; only exits the if statement, not the loop
jolm1x wrote:
To my knowledge the break; only exits the if statement, not the loop

No. break has no interaction with if blocks at all. It will exit the innermost loop that it's in, and if statements are not loops.

@OP I'd recommend stepping through your code with a debugger, so that you can see exactly where the logic in the execution differs from what you expect.
Last edited on
Topic archived. No new replies allowed.