Help with mastermind

ok so I am using this code below to figure out how to get the computer to guess a random 5 digit number and the player has to put in there own number and i got to let them know if they got any correct numbers and if they got any correct numbers in the right spot. i got the game loop implemented and i got it working all the way up until the telling them how many they got right this is where i am super confused can someone please guide me in the right direction

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
 
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>

using namespace std;

int main()
{
	int size = 5;
	int range = 9;
	char lowest = '0';
	string guess;
	int number;
	int correct;
	int position;
	bool gameover = false;

	srand(time(0));

	string answer = "";
	for (int i = 0; i < size; i++)
	{
		char ch = lowest + rand() % range;
		answer += ch;
	}


	number = 1;
	correct = 0;
	position = 0;

	while (!gameover)
	{
		cout << "Guess #" << number << ": Enter 5 numbers that are '0' through '9': ";
		cout << answer;
		cout << "\n";
		cin >> guess;

		if (guess == answer)
		{
			cout << "Right!  It took you " << number << " move";
			if (number != 1) cout << "s";
			cout << "." << endl;
			gameover = true;
			
		}
		
		for (int i = 0; i < 5; i++){
			if (guess[i] == answer[i]){
				correct = ;

			}
			
		}

		while (position < size)
		{
			if (size == position) correct++;
			position++;
		}
		number++;


		cout << "Correct position: " << position << endl;
		cout << "Correct letter:   " << correct << endl;


	}

	
	cout << "GAME OVER\n\n";

	system("pause");
	return 0;
}

You could do something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int const max = 99999;
int const min = 10000;

int answer = rand() % (max - min + 1) + min;

int attempt = 1;
int guess = 0;
while(guess != answer)
{
    std::cout << "Guess attempt #" << attempt << std::endl;
    do
    {
        std::cout << "Please enter a guess(" << min << '-' << max << "): ";
        if(!cin) //error with input
        {
            std::cin.clear();
            std::cin.ignore(1024, '\n'); //or use std::numeric_limit
        } //edited to add this forgot last time
    } while(!(std::cin >> guess) || guess < min || guess > max);

    ++attempt;
}

std::cout << "Congrats you guessed the number in " << attempt << " guesses!" << std::endl;
Last edited on
But my issue is telling hte person how many numebrs of the 5 digit number they guessed is actually correct

say the number is 56478

and u guessed 423568

i have to be able to tell u that 3 numbers that u guessed is in the number and 1 number is in the right spot
oh sorry I missed that part. You're going to have to iterate through both of them and keep track with two flag variables. Or you could use the .find function
Last edited on
Ahh the find function, I am new to iterators and have only used them for vectors. so I can use them for strings as well?
I have to make it like this

Have your computer generate a random 5 digit number, as a string. Then the user must guess the number. Using the code:

1 = implies right Number, Right Place.
2 = implies right Number, Wrong Place.
0 = implies Wrong Number.

So the computer decides on 12345; the user guesses 11235; the computer should respond with 10221. Hint: Watch out for a double number like 11 when there is only one.

I have this as the code

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


struct fields{
	int size = 5;;
	int range = 9;
	char lowest = '0';
	string guess;
	string answer;
	int number;
	int correct;
	int position;
	bool gameover = false;

};

void gameplay(fields & info);

int main()
{
	fields game;

	gameplay(game);

	

	system("pause");
	return 0;
}

void gameplay(fields & info){

	srand(time(0));
	

	info.answer = "";
	for (int i = 0; i < info.size; i++)
	{
		char ch = info.lowest + rand() % info.range;
		info.answer += ch;
	}


	info.number = 1;
	info.correct = 0;
	info.position = 0;

	while (!info.gameover)
	{
		cout << "Guess #" << info.number << ": Enter 5 numbers that are '0' through '9': ";
		cout << info.answer;
		cout << "\n";
		cin >> info.guess;

		if (info.guess == info.answer)
		{
			cout << "Right!  It took you " << info.number << " move";
			if (info.number != 1) cout << "s";
			cout << "." << endl;
			info.gameover = true;

		}

		
		int correctNumbers = 0;
		
		for (char const &ch : info.guess) //iterate over string you can do anyway you want
		{
			if (info.answer.find(ch) != string::npos)
			{
				++correctNumbers;
			}
		}

		int const digits = 5;
		int correctPositions = 0;
		
		int correctPosition[digits];
		int test = 0;

		
			for (int i = 0; i < digits; ++i)
			{
				if (info.answer[i] == info.guess[i])
				{
					++correctPositions;
				}
				if (info.answer[i] == info.guess[i]){

					correctPosition[i] = 2;
					cout << correctPosition[i];
				}
				

				
			}
		

		while (info.position < info.size)
		{
			if (info.size == info.position) info.correct++;
			info.position++;
		}
		info.number++;


		cout << "\nCorrect position: " << correctPositions << endl;
		cout << "Correct letter:   " << correctNumbers << endl;
		


	}


	cout << "GAME OVER\n\n";
}



I go tit where it puts a 2 if it is the right number in the right place and a 0 if the number is not part of it,,h i cant figure out how to include the 1 though can i please get some help
Topic archived. No new replies allowed.