Skip checking and getline problem.

I seem to be experiencing two big problems that I have no clue on how to solve. The first of the problem in my script is, after i enter the desired level of difficulty, the program also registers it as something I entered for the getline{cin, user), is there anyway to fix this? Another problem I've experienced is that when in the game, I try to check for two things, number of correct numbers in the RIGHT position and number of correct numbers in the WRONG position. However, I don't know how to make it stop checking the same thing over and over. For example, if the "Mastermind" code is: "1234" and I enter in as the codebreaker: "4444", the program will output "4444 1/3" and therefore reading the 4s as a correct number in wrong position, but in total there should only be one 4 in the correct "Mastermind" 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
#include <iostream>
#include <time.h>
#include <cstdlib>
#include <string>

using namespace std;

int level;
int difficulty(){
	switch(level){
	case 1:
		cout << "Enjoy your relaxing game." << endl;
		break;
	case 2:
		cout << "Put your skills to the test, you have 18 turns left!" << endl;
		break;
	case 3:
		cout << "A race against time, there will be no escape in your last 12 turns!!" << endl;
		break;
	}
	return 0;
}
int introduction(){
	cout << "Welcome to Mastermind." << endl;
	cout << "This is a race against time!" << endl;
	cout << "Each time you enter a code, I will give you a hint!" << endl;
	cout << "Every code has 4 digits, and each digit is a number." << endl;
	cout << "The first hint indicates how many digits that are in the right position and with the correct number." << endl;
	cout << "The second hint indicates how many digits that are right but in the wrong position." << endl;
	cout << "Try your best code breaker!" << endl << endl;
	cout << "1 - Beginner" << endl;
	cout << "2 - Intermediate" << endl;
	cout << "3 - Impossible" << endl;
	cout << "Enter the number corresponding with the level of difficulty desired: ";

	return 0;
}

// This is all the introduction, skip this part.


int random(){
	return rand() % 10;
}
int main() {
	introduction();
	cin >> level;
	difficulty();
	string user;
	int turns = 0, complete = 0, Mastermind[4], input[4];
	srand(time(0));
	for(int i = 0; i < 4; i++){
		Mastermind[i] = random();
	}

// The problem is here where I have to enter in the level of difficulty I want for my game but it also registers as me entering a code into getline(cin, user) or so it runs the loop once already.

	while(complete != 1){
		int pos = 0, num = 0;
		turns += 1;
		cout << "Please enter your guess for the secret code: ";
		getline(cin, user);
		for(int i =0; i < 4; i++){
		input[i] = user[i];
		input[i] -= 48;
		}
		if((input[0] == Mastermind[0]) || (input[0] == Mastermind[1]) || (input[0] == Mastermind[2]) || (input[0] == Mastermind[3])){
			num += 1;
		}

// This is the biggest problem of them all right here ^, I don't know how to
//make it only check for a number once and skip that afterwards. For example,
//if the "Mastermind" code is: "1234" and I enter in as the codebreaker:
//"4444", the program will output "4444 1/3" and therefore reading the 4s as a
//correct number in wrong position, but in total there should only be one 4 in
//the correct "Mastermind" code.

		for(int i = 0; i <4; i++){
			if(input[i] == -48){
				input[i] = 0;
				cout << Mastermind[i] << endl;
			}
			if(input[i] == Mastermind [i]){
				num -= 1;
				pos += 1;
			}
		}
		if(pos == 4){
			complete = 1;
			cout << "Congratulations!" << endl;
			cout << "You cracked the code in only " << turns << " tries!" << endl;
		}
		else{
			for(int i = 0; i <4; i++){
				cout << input[i];
			}
			cout << " " << pos << "/" << num << endl << endl;
		}
		if((pos != 4) && (level == 2) && (turns == 19)){
			cout << "**GAME OVER**" << endl;
			complete = 1;
		}
		if((pos != 4) && (level == 3) && (turns == 13)){
			cout << "**GAME OVER**" << endl;
			complete = 1;
		}
	}
	return 0;
}
Last edited on
as for the 1st problem try using std::cin.ignore(); after reading the difficulty level. This is because, it seems that the new line character remains in your input stream after the int is read.

EDIT:
As for the second problem, one simple solution can be to have another array of correctNumbers storing the information of whether the i'th input is correct and in correct position. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//1st check how many of them are correct values in correct positions
bool correctNumbers[4] = {};
for (int i = 0; i<4; i++) {
  if (input[i] == Mastermind[i]) {
    pos++;
    correctNumbers[i] = 1;
  }
}

//Now check how many are correct but in wrong positions
for (int i=0; i<4; i++) {
  if (!correctNumbers[i]) { // only for those inputs which are not in their correct positions
     //    ... do whatever
  }
}
Last edited on
Thank you for letting me know about std::cin.ignore();. I did some research and learned how to use it, as for the second problem, thanks for your feedback, I'm working something out at the moment.
Topic archived. No new replies allowed.