Loop not working correctly

I've got a do while loop but it's not working correctly for 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
  #include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include <time.h>
#include <vector>
#include <algorithm>


using namespace std;


string convert_int_to_string(int generated[5])
{
	stringstream generated_num[5];
	
	generated_num[0] << generated[0];
	generated_num[1] << generated[1];
	generated_num[2] << generated[2];
	generated_num[3] << generated[3];
	generated_num[4] << generated[4];
	
	return generated_num[5].str();
};

string convert_guess_int_to_string(int guess[5])
{
	stringstream players_guess[5];
	
	players_guess[0] << guess[0];
	players_guess[1] << guess[1];
	players_guess[2] << guess[2];
	players_guess[3] << guess[3];
	players_guess[4] << guess[4];
	
	return players_guess[5].str();
};


void welcome();									// Defining function
void instructions();							// Defining function
int generate();									// Defining function
int user_guess();								// Defining function
string convert_tostring();						// Defining function
void display();									// Defining function

void welcome()
{
	cout << "Welcome to mastermind.\n " ;
}

void instructions ()
{
	cout << "You will be prompted to enter a # 0-9 for 5 characters. \n ";
	cout << "The computer will see if they match. \n ";
	cout << "0 will imply that the number is not used. \n ";
	cout << "1 will imply that the number is the correct and location. \n ";
	cout << "2 will imply that the number is used but in a different location. \n ";
	cout << "You will have 10 tries to guess the correct number. \n";

}	

int main()
{
srand((unsigned)time(NULL));					// initializing random generator
	
int generated_num[5], guess[5], x;


int a = rand()%10;									// Creating 1st random #
int b = rand()%10;									// Creating 1st random #
int c = rand()%10;									// Creating 1st random #
int d = rand()%10;									// Creating 1st random #
int e = rand()%10;									// Creating 1st random #

generated_num[0] = a;
generated_num[1] = b;
generated_num[2] = c;
generated_num[3] = d;
generated_num[4] = e;

int q, r, s, t, u;


int number_of_guesses = 10;
x = 0;

	welcome();									// Calling the welcome function
	instructions();								// Calling the instructions function
	
	do
	{
	
	{
	cout << "Please enter your 1st digit guess. ";
	cin >> guess[0];
	
	cout << "Please enter your 2nd digit guess. ";
	cin >> guess[1];
	
	cout << "Please enter your 3rd digit guess. ";
	cin >> guess[2];
	
	cout << "Please enter your 4th digit guess. ";
	cin >> guess[3];
	
	cout << "Please enter your 5th digit guess. ";
	cin >> guess[4];
	
	if (guess[0] == generated_num[0])
	 {
	 	q = 1;
	  }
	if (guess[0] == generated_num[1] || guess[0] == generated_num[2] || guess[0] == generated_num[3] || guess[0] == generated_num[4])
	 {
	 	q = 2;
	  }
	if (guess[0] != generated_num[0] && guess[0] != generated_num[1] && guess[0] != generated_num[2] && guess[0] != generated_num[3] && guess[0] != generated_num[4])
	{
		q = 0;
	}
	
	if (guess[1] == generated_num[1])
	 {
	 	r = 1;
	  }
	if (guess[1] == generated_num[0] || guess[1] == generated_num[2] || guess[1] == generated_num[3] || guess[1] == generated_num[4])
	 {
	 	r = 2;
	  }
	if (guess[1] != generated_num[0] && guess[1] != generated_num[1] && guess[1] != generated_num[2] && guess[1] != generated_num[3] && guess[1] != generated_num[4])
	{
		r = 0;
	}
	
	if (guess[2] == generated_num[2])
	 {
	 	s = 1;
	  }
	if (guess[2] == generated_num[0] || guess[2] == generated_num[1] || guess[2] == generated_num[3] || guess[2] == generated_num[4])
	 {
	 	s = 2;
	  }
	if (guess[2] != generated_num[0] && guess[2] != generated_num[1] && guess[2] != generated_num[2] && guess[2] != generated_num[3] && guess[2] != generated_num[4])
	{
		s = 0;
	}
	
	if (guess[3] == generated_num[3])
	 {
	 	t = 1;
	  }
	if (guess[3] == generated_num[0] || guess[3] == generated_num[1] || guess[3] == generated_num[2] || guess[3] == generated_num[4])
	 {
	 	t = 2;
	  }
	if (guess[3] != generated_num[0] && guess[3] != generated_num[1] && guess[3] != generated_num[2] && guess[3] != generated_num[3] && guess[3] != generated_num[4])
	{
		t = 0;
	}
	
	if (guess[4] == generated_num[4])
	 {
	 	u = 1;
	  }
	if (guess[4] == generated_num[0] || guess[4] == generated_num[1] || guess[4] == generated_num[2] || guess[4] == generated_num[3])
	 {
	 	u = 2;
	  }
	if (guess[4] != generated_num[0] && guess[4] != generated_num[1] && guess[4] != generated_num[2] && guess[4] != generated_num[3] && guess[4] != generated_num[4])
	{
		u = 0;
	}
	  
	if (q == 1, r == 1, s == 1, t == 1, u == 1)
	
	{
		cout << "You are a mastermind and have won" << endl;
	}
	
	else
	{
		cout <<  "These are the current results " << q << r << s << t << u  ;
	}
	
	}
	}
	while (x < 10, x++ || (q == 1, r == 1, s == 1, t == 1, u == 1) );
	
	{
		cout << "The numbers you needed were "	<<a<<b<<c<<d<<e  ;
	}
	


return 0;



}


I want the do while loop to loop 10 times or end if the conditions are met but it goes past that and displays the numbers I needed.
The comma operator ignores the result of the first operand and returns the result of the second operand so line 189 is equivalent to:

 
while (x++ || u == 1);

Maybe you did mean to use the logical AND operator && instead?

If u != 1 then this loop will not run any time because x++ will return the value of x before the increment, which is 0 which will be treated as false. If you know how to use for loops you might consider using that instead because it's more suitable when you want to loop a certain number of times. If you use a while loop, at least put the x++ inside the loop body.

If you want line 191-193 to be part of the loop you have to remove the semicolon from the end of line 189.
Last edited on
I see what you said and changed it so that it's the logical && which I changed. I wanted the conditions to be either they got all the guess correct or x < 10 which I think i've fixed now. I want it to display lines 194-196 once x gets over 9 or they don't match the numbers.

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

#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
#include <math.h>
#include <time.h>
#include <vector>
#include <algorithm>


using namespace std;


string convert_int_to_string(int generated[5])
{
	stringstream generated_num[5];
	
	generated_num[0] << generated[0];
	generated_num[1] << generated[1];
	generated_num[2] << generated[2];
	generated_num[3] << generated[3];
	generated_num[4] << generated[4];
	
	return generated_num[5].str();
};

string convert_guess_int_to_string(int guess[5])
{
	stringstream players_guess[5];
	
	players_guess[0] << guess[0];
	players_guess[1] << guess[1];
	players_guess[2] << guess[2];
	players_guess[3] << guess[3];
	players_guess[4] << guess[4];
	
	return players_guess[5].str();
};


void welcome();									// Defining function
void instructions();							// Defining function
int generate();									// Defining function
int user_guess();								// Defining function
string convert_tostring();						// Defining function
void display();									// Defining function

void welcome()
{
	cout << "Welcome to mastermind.\n " ;
}

void instructions ()
{
	cout << "You will be prompted to enter a # 0-9 for 5 characters. \n ";
	cout << "The computer will see if they match. \n ";
	cout << "0 will imply that the number is not used. \n ";
	cout << "1 will imply that the number is the correct and location. \n ";
	cout << "2 will imply that the number is used but in a different location. \n ";
	cout << "You will have 10 tries to guess the correct number. \n";

}	

int main()
{
srand((unsigned)time(NULL));					// initializing random generator
	
int generated_num[5], guess[5], x;


int a = rand()%10;									// Creating 1st random #
int b = rand()%10;									// Creating 1st random #
int c = rand()%10;									// Creating 1st random #
int d = rand()%10;									// Creating 1st random #
int e = rand()%10;									// Creating 1st random #

generated_num[0] = a;
generated_num[1] = b;
generated_num[2] = c;
generated_num[3] = d;
generated_num[4] = e;

int q, r, s, t, u;


int number_of_guesses = 10;
x = 0;

	welcome();									// Calling the welcome function
	instructions();								// Calling the instructions function
	
	do
	{
	
	{
	cout << "Please enter your 1st digit guess. ";
	cin >> guess[0];
	
	cout << "Please enter your 2nd digit guess. ";
	cin >> guess[1];
	
	cout << "Please enter your 3rd digit guess. ";
	cin >> guess[2];
	
	cout << "Please enter your 4th digit guess. ";
	cin >> guess[3];
	
	cout << "Please enter your 5th digit guess. ";
	cin >> guess[4];
	
	if (guess[0] == generated_num[0])
	 {
	 	q = 1;
	  }
	if (guess[0] == generated_num[1] || guess[0] == generated_num[2] || guess[0] == generated_num[3] || guess[0] == generated_num[4])
	 {
	 	q = 2;
	  }
	if (guess[0] != generated_num[0] && guess[0] != generated_num[1] && guess[0] != generated_num[2] && guess[0] != generated_num[3] && guess[0] != generated_num[4])
	{
		q = 0;
	}
	
	if (guess[1] == generated_num[1])
	 {
	 	r = 1;
	  }
	if (guess[1] == generated_num[0] || guess[1] == generated_num[2] || guess[1] == generated_num[3] || guess[1] == generated_num[4])
	 {
	 	r = 2;
	  }
	if (guess[1] != generated_num[0] && guess[1] != generated_num[1] && guess[1] != generated_num[2] && guess[1] != generated_num[3] && guess[1] != generated_num[4])
	{
		r = 0;
	}
	
	if (guess[2] == generated_num[2])
	 {
	 	s = 1;
	  }
	if (guess[2] == generated_num[0] || guess[2] == generated_num[1] || guess[2] == generated_num[3] || guess[2] == generated_num[4])
	 {
	 	s = 2;
	  }
	if (guess[2] != generated_num[0] && guess[2] != generated_num[1] && guess[2] != generated_num[2] && guess[2] != generated_num[3] && guess[2] != generated_num[4])
	{
		s = 0;
	}
	
	if (guess[3] == generated_num[3])
	 {
	 	t = 1;
	  }
	if (guess[3] == generated_num[0] || guess[3] == generated_num[1] || guess[3] == generated_num[2] || guess[3] == generated_num[4])
	 {
	 	t = 2;
	  }
	if (guess[3] != generated_num[0] && guess[3] != generated_num[1] && guess[3] != generated_num[2] && guess[3] != generated_num[3] && guess[3] != generated_num[4])
	{
		t = 0;
	}
	
	if (guess[4] == generated_num[4])
	 {
	 	u = 1;
	  }
	if (guess[4] == generated_num[0] || guess[4] == generated_num[1] || guess[4] == generated_num[2] || guess[4] == generated_num[3])
	 {
	 	u = 2;
	  }
	if (guess[4] != generated_num[0] && guess[4] != generated_num[1] && guess[4] != generated_num[2] && guess[4] != generated_num[3] && guess[4] != generated_num[4])
	{
		u = 0;
	}
	  
	if (q == 1, r == 1, s == 1, t == 1, u == 1)
	
	{
		cout << "You are a mastermind and have won" << endl;
	}
	
	else
	{
		cout <<  "These are the current results " << q << r << s << t << u  ;
	}
	
	}
	while 
	
	(x < 10, x++ || (q == 1 && r == 1 && s == 1 && t == 1 && u == 1) );
	}
	
	
	{
		cout << "The numbers you needed were "	<<a<<b<<c<<d<<e  ;
	}
	


return 0;



}


Also when I add the while statement into the loop I get compile error for lines 193-195
It's hard to follow your code because the indentation is a bit off. I didn't realize the while was part of a do-while loop.

The error is because the do-while loop no longer has a while part.

You still have a comma inside the loop condition and in the if condition on line 177.
Here's some spacing and bracket corrections that may help. One of the great features of Code::Blocks is that any code can easily be correctly spaced and formatted by right-clicking highlighted text and selecting AStyle format.
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
int main()
{
    srand((unsigned)time(NULL));					// initializing random generator
    int generated_num[5], guess[5], x=0;

    int a = rand()%10;									// Creating 1st random #
    int b = rand()%10;									// Creating 1st random #
    int c = rand()%10;									// Creating 1st random #
    int d = rand()%10;									// Creating 1st random #
    int e = rand()%10;									// Creating 1st random #

    generated_num[0] = a;
    generated_num[1] = b;
    generated_num[2] = c;
    generated_num[3] = d;
    generated_num[4] = e;

    int q, r, s, t, u;
    int number_of_guesses = 10;

    welcome();									// Calling the welcome function
    instructions();								// Calling the instructions function
    do
    {
        cout << "Please enter your 1st digit guess. ";
        cin >> guess[0];

        cout << "Please enter your 2nd digit guess. ";
        cin >> guess[1];

        cout << "Please enter your 3rd digit guess. ";
        cin >> guess[2];

        cout << "Please enter your 4th digit guess. ";
        cin >> guess[3];

        cout << "Please enter your 5th digit guess. ";
        cin >> guess[4];

        if (guess[0] == generated_num[0])
            q = 1;
        if (guess[0] == generated_num[1] || guess[0] == generated_num[2] || guess[0] == generated_num[3] || guess[0] == generated_num[4])
            q = 2;
        if (guess[0] != generated_num[0] && guess[0] != generated_num[1] && guess[0] != generated_num[2] && guess[0] != generated_num[3] && guess[0] != generated_num[4])
            q = 0;
        if (guess[1] == generated_num[1])
            r = 1;
        if (guess[1] == generated_num[0] || guess[1] == generated_num[2] || guess[1] == generated_num[3] || guess[1] == generated_num[4])
            r = 2;
        if (guess[1] != generated_num[0] && guess[1] != generated_num[1] && guess[1] != generated_num[2] && guess[1] != generated_num[3] && guess[1] != generated_num[4])
            r = 0;
        if (guess[2] == generated_num[2])
            s = 1;
        if (guess[2] == generated_num[0] || guess[2] == generated_num[1] || guess[2] == generated_num[3] || guess[2] == generated_num[4])
            s = 2;
        if (guess[2] != generated_num[0] && guess[2] != generated_num[1] && guess[2] != generated_num[2] && guess[2] != generated_num[3] && guess[2] != generated_num[4])
            s = 0;
        if (guess[3] == generated_num[3])
            t = 1;
        if (guess[3] == generated_num[0] || guess[3] == generated_num[1] || guess[3] == generated_num[2] || guess[3] == generated_num[4])
            t = 2;
        if (guess[3] != generated_num[0] && guess[3] != generated_num[1] && guess[3] != generated_num[2] && guess[3] != generated_num[3] && guess[3] != generated_num[4])
            t = 0;
        if (guess[4] == generated_num[4])
            u = 1;
        if (guess[4] == generated_num[0] || guess[4] == generated_num[1] || guess[4] == generated_num[2] || guess[4] == generated_num[3])
            u = 2;
        if (guess[4] != generated_num[0] && guess[4] != generated_num[1] && guess[4] != generated_num[2] && guess[4] != generated_num[3] && guess[4] != generated_num[4])
            u = 0;
        if (q == 1, r == 1, s == 1, t == 1, u == 1)
            cout << "You are a mastermind and have won" << endl;
        else
            cout <<  "These are the current results " << q << r << s << t << u  ;
    } while (x < 10, x++ || (q == 1 && r == 1 && s == 1 && t == 1 && u == 1) );
    cout << "The numbers you needed were "<<a<<b<<c<<d<<e  ;
    return 0;
}

This does away with several of the unneeded brackets and makes the code more readable, although I haven't tried it yet. You may find it easier to modify the code above using the previous suggestions now that the extra brackets are eliminated, and the existing ones are spaced for easy identification, though. Your functions are fine as they are (Where is the display function?), but you can ditch the trailing semicolon at the end of each (Example: Line 25 of your original post).
Last edited on
Topic archived. No new replies allowed.