c++ array

trying to make a binary code breaker where computer generates random for digits made up of 0 and 1 (ex, 1010, 1001)
and now i have to guess it.
and computer tells how many numbers are correct or wrong.
if i dont get the answer within 5 attemps, i lose.

this is what i have done so far.
first problem i have with this is that
a=rand()%2
b=rand()%2
...
doesnt really generate random number.
it somehow always gives me 1100.

while loop, when i put the wrong number, instead of going back, and have me try more, it just prints # of your number are wrong infinite time.

so what did i do with while loop there?

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
#include <string>
#include <iostream>
using namespace std;

void main()
{
	int a,b,c,d,n,e,f,g,h,z;
	a=rand()%2;
	b=rand()%2;
	c=rand()%2;
	d=rand()%2;
	cout << a << b << c << d << endl;
	
	cout << "enter 4 numbers between 0 and 1" << endl;
	cin >> e >> f >> g >> h;
	bool answer = false;

	while (!answer)
	{	
	if ((a==e && b==f && c==g && d!=h) || (a==e && b==f && c!=g && d==h) || (a==e && b!=f && c==g && d==h) || (a!=e && b==f && c==g && d==h))
	{ cout << " one of your number is wrong" << endl;
	}
	
	if ((a==e && b==f && c!=g && d!=h) || (a==e && b!=f && c==g && d!=h) || (a!=e && b==f && c==g && d!=h) || (a!=e && b==f && c!=g && d==h) || (a!=e && b!=f && c==g && d==h)|| (a==e && b!=f && c!=g && d==h))
	{ cout << "two of your number are wrong" << endl;
	}
	
	if ((a==e&& b!=f&& c!=g&& d!=h) || (a!=e&& b==f&& c!=g && d!=h) || (a!=e&& b!=f&& c!=g && d==h) || (a!=e&& b!=f&& c==g && d!=h))
	{cout << " three of your number are wrong" << endl;
	}
	
	if ((a!=e&& b!=f&& c!=g&& d!=h))
	{ cout << " all of your number are wrong" << endl;
	}

	if (a==e && b==f && c==g && d==h)
	{cout << " you got it right" << endl;
	answer = true;}
	}
	
	
	system("pause");

}
Your code is far too complicated, you should use an array for this.

doesnt really generate random number.
it somehow always gives me 1100.

That's because you didn't seed the pseudo random number generator.
See here: http://cplusplus.com/reference/clibrary/cstdlib/srand/
unfortunally.. i dont know an array.

only thing i can think of is to use while or if statements
Well, just look at the next chapter in your books. Chance is that it will cover arrays, as it is one of the very basics. If not, the page in the tutorial should help you:
http://www.cplusplus.com/doc/tutorial/arrays/
Last edited on
Topic archived. No new replies allowed.