Binary Code Breaker game... Stuck.

Intro info so you know where im at

Ok, well let me explain my issues first. I am taking a class and have understood everything up until these past two chapters. The reason for this is partly because of my instructor not really understanding why I don't get something and giving me the old "well that's how im gonna explain it now you're on your own" speel. And, partly because we have the worst book I could ever imagine for learning this. The book is one of the college specific books with chapters from other books combined together, and it doesn't really explain much. To clarify, the book gives an example briefly hints at what the code is doing and then gives assignments.

So, I have made it through to this point but without knowing what I am able to do with certain functions, etc, im at a loss. So far we have covered the basic stuff, and some of these where just mentioned and not even gone into. ex, bool, char.

We have now went through loops and arrays and this is where I am really lost because of lack of info. The only thing I have almost fully grasped from these two concepts is do-while loops.

Onto my problem

So, I have made it through all of the assignments so far by looking at other code (and even using suggestions I haven't really understood) and reading what I can on the internet, but now I am pretty much stuck on this one.

So on my last assignment here I am tasked with creating a Binary Code Breaker game. The object of the game is the player is to guess a 4 digit binary code in 5 tries, and when they guess, if they are wrong, the program should tell them how many digit they had in the correct place.

I want to be very claer that this is not me asking you to do my homework. This is me reaching out for help because this is really something I want to be able to do and understand. Spending 10 1/2 hours on a simple program like I did on the last one isn't helping me at all!

So far I have written a pseudo pseudo code.

Write includes
Welcome User to game
list codes to an array
prompt user to input a guess
if user guesses part of code:
program outputs number of digits that were input correctly
set a counter to allow for five tries
if user guesses correctly or try count limit is reached:
end sequence
prompt user to play again (y or n?)

My code so far

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

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(0)));
	
	const int CODES = 10;
	const int BIN[CODES] =
	{
		0100
		1010
		1110
		1011
		0001
		0010
		0000
		1111
		1000
		0100
	}
		
	int outCode = (rand() % CODES);
	string secretCode = BIN[outCode];
	int guess;
	
	do
	{
		cout << "Enter you guess: ";
		cin >> guess;
		
			if (guess != secretCode)
			{
				do
				{
					for(int i = 0; i < secretCode; i++ )
					{
						
					}
}	
	





So far it looks like I don't have much but unfortunately this is were im stuck.

Specifically I have two questions.

1. for(int i = 0; i < secretCode; i++ )

Can someone explain this thoroughly to me I understand that it is declaring the variable integer i, and that it is incrementing i by 1, but how do this work in specific situations?

2. This is the point in the code where im really stuck.

How do I get the program to output the number of correct digits from the user input?

Thank you for the help. If there is anything else you see I could be doing in a more efficient easier way please point it out and don't be afraid to explain why.

Also can anyone recommend a good bookstore beginner c++ book (like c++ for dummies something along those line) that has thorough explainations and is interactive?
Last edited on
closed account (28poGNh0)
for(int i = 0; i < secretCode; i++ )
This is a for loop works like this

excute 10(9-0) time the expression
cout << "I am the line " << i+1 << endl;


exemple

1
2
for(int i=0;i<10;i++)
        cout << "I am the line " << i+1 << endl;


we do a lot of things with for loops check this link http://www.cplusplus.com/forum/beginner/78298/

in your case .. not gonna works check code under line 48

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
# include <iostream>
# include <conio.h>
# include <cstdlib>
# include <ctime>
using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));

    const int CODES = 10;

    string BIN[CODES] =
	{
		"0100",
		"1010",
		"1110",
		"1011",
		"0001",
		"0010",
		"0000",
		"1111",
		"1000",
		"0100"
	};

    const int guessNum = 5;
    string guess;

    while(true)
    {
        cout << "You want to play y/n " << endl << endl;
        char decision = getch();

        if(decision == 'y')
        {
            int outCode = (rand() % CODES);
            string secretCode = BIN[outCode];

            for(int i=0;i<guessNum;i++)
            {
                int counter = 0;

                cout << "Still you have " << 5-i << " guess(es)" << endl;
                cout << "Enter you guess -> ";
                getline(cin,guess);

                for(size_t i=0;i<guess.size();i++)
                {
                    if(guess[i]==secretCode[i])
                    {
                        cout << guess[i];
                        ++counter;
                    }
                    else cout << "*";
                }cout << endl;
                if(counter==4)
                {
                    cout << "You won " << endl << endl;
                    break;
                }
            }
        }else if(decision == 'n')break;
    }

    cout << "End of program " << endl;

    return 0;
}


I did not give much explication but if you want anything just ask
I hope it helps
Hi techno01,

Thank you so much yes it helps very much.

Two questions.

1. line 46 getline(cin,guess);

That gives me an error: " 'getline' identifier not found." I haven't used this before either can you tell me what it does?

2. Less of a question. Im gonna see if I have an understanding of whats going on in this section. Can you let me know if im right or wrong?

1
2
3
4
5
6
7
8
9
 for(size_t i=0;i<guess.size();i++)
                {
                    if(guess[i]==secretCode[i])
                    {
                        cout << guess[i];
                        ++counter;
                    }
                    else cout << "*";
                }cout << endl;


Ok, so starting from the top (line 48).

size_t i=0 : is declaring i as a size variable and setting the size to zero.

i<guess.size(); i++ : is saying that as long as i is less than the size of the guess increment i by 1.

1
2
3
4
 if(guess[i]==secretCode[i])
                    {
                        cout << guess[i];
                        ++counter;


This is saying that however many numbers in the guess equal numbers in the secretCode will be incremented by i and output as a value. Followed by the counter being increased?

1
2
   else cout << "*";
                }cout << endl;


And, this is saying otherwise output an asterisk?

Again thank you.
Awesome, I fixed the error. It was just # include <string>

Now that it is running I see what the large portion (the 2nd question I asked) is doing.

Could you still explain to me what getline(cin,guess); is doing? I still am unsure as to that.

Thanks.
closed account (28poGNh0)
You're welcome any time, and Yes you're totly understood the program

1
2
3
4
if(guess[i]==secretCode[i])
                    {
                        cout << guess[i];
                        ++counter;


about this piece of code the idea behind it is when we get counter = 4 means that we got all digits matched
Topic archived. No new replies allowed.