Please help

Ok, so I've got a lil' two player word guessing game where the first player enters a 5 letter word into the program and then the second player attempts to figure out the word by guessing letters. The second player has only 10 incorrect guesses before losing the game. I need to change this program so that if the second player guesses a letter that they've ALREADY guessed an error message will display and they are NOT penalized for it, (the numIncorrect should not raise +1)
And here's 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
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
	string origWord = "";
	string letter = "";
	char dashReplaced = 'N';
	char gameOver = 'N';
	int numIncorrect = 0;
	string displayWord = "-----";
	
	//get original word
	while (origWord.length() != 5)
	{
		cout << "Enter a 5-letter word in uppercase: ";
		getline(cin, origWord);
	} //end while
	
	system("cls"); //clear the screen
	
	//start guessing
	cout << "Guess this word: " << displayWord << endl;
	while (gameOver == 'N')
	{
		cout << "Enter an uppercase letter: ";
		cin >> letter;
		
		//search for the letter in the original word
		for (int x = 0; x < 5; x += 1)
		{
			//if the current character matches the letter, replace the corresponding dash in the displayWord variable and then set the dashReplaced variable to 'Y'
			if (origWord.substr(x, 1) == letter)
			{
				displayWord.replace(x, 1, letter);
				dashReplaced = 'Y';
			} //end if
		} //end for
		// if a dash was replaced, check whether the displayWord variable contains another dash
		if (dashReplaced == 'Y')
		{
			//if the displayWord variable does not contain any dashed, the game is over
			if (displayWord.find("-", 0) == -1)
			{
				gameOver = 'Y';
				cout << endl << "Yes, the word is " << origWord << endl;
				cout << "Great guessing!" <<endl;
			}
			else //otherwise, continue guessing
			{
				cout << endl << "Guess this word: " << displayWord << endl;
				dashReplaced = 'N';
			} //end if
		}
		else //processed when dashReplaced contains 'N'
		{
			//add 1 to the number of incorrect guesses
			numIncorrect += 1;
			//if the number of incorrect guesses is 10, the game is over
			if (numIncorrect == 10)
			{
				gameOver = 'Y';
				cout << endl << "Sorry the word is " << origWord << endl;
			} //end if
		} //end if
	} //end while
	return 0;
} //end of main function 
The way I see it, you could store all of the guesses in the vector. Then, every iteration of the while loop, check f the inputted guess matches any element in the vector.

Below is an edited version of your code.
Added function on starts on line 36.

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

int main()
{
    string origWord = "";
    string letter = "";
    char dashReplaced = 'N';
    char gameOver = 'N';
    int numIncorrect = 0;
    string displayWord = "-----";
    vector <string> previousGuesses;
    
    //get original word
    while (origWord.length() != 5)
    {
        cout << "Enter a 5-letter word in uppercase: ";
        getline(cin, origWord);
    } //end while
    
    system("cls"); //clear the screen
    
    //start guessing
    cout << "Guess this word: " << displayWord << endl;
    while (gameOver == 'N')
    {
        cout << "Enter an uppercase letter: ";
        cin >> letter;
        
        /* Loops through every element in the vector.
         * previousGuesses.size() denotes how many elements are in the vector
         */
        for (int i = 0; i < previousGuesses.size(); i++)
        {
            // Check if the guess is in the list of
            if (letter == previousGuesses[i])
            {
                cout << "\nYou already entered that letter! " << endl;
                cout << "Please enter a different letter: " << endl;
                cin >> letter;
            }
        }
        
        // Add the guess to the list of guesses
        previousGuesses.push_back(letter);
        
        //search for the letter in the original word
        for (int x = 0; x < 5; x += 1)
        {
            //if the current character matches the letter, replace the corresponding dash in the displayWord variable and then set the dashReplaced variable to 'Y'
            if (origWord.substr(x, 1) == letter)
            {
                displayWord.replace(x, 1, letter);
                dashReplaced = 'Y';
            } //end if
        } //end for
        // if a dash was replaced, check whether the displayWord variable contains another dash
        if (dashReplaced == 'Y')
        {
            //if the displayWord variable does not contain any dashed, the game is over
            if (displayWord.find("-", 0) == -1)
            {
                gameOver = 'Y';
                cout << endl << "Yes, the word is " << origWord << endl;
                cout << "Great guessing!" <<endl;
            }
            else //otherwise, continue guessing
            {
                cout << endl << "Guess this word: " << displayWord << endl;
                dashReplaced = 'N';
            } //end if
        }
        else //processed when dashReplaced contains 'N'
        {
            //add 1 to the number of incorrect guesses
            numIncorrect += 1;
            //if the number of incorrect guesses is 10, the game is over
            if (numIncorrect == 10)
            {
                gameOver = 'Y';
                cout << endl << "Sorry the word is " << origWord << endl;
            } //end if
        } //end if
    } //end while
    return 0;
} //end of main function 


If you're unfamiliar with vectors, there's an article about it here: http://www.cplusplus.com/reference/vector/vector/

Hope it helps,
VX
Topic archived. No new replies allowed.