String Coding

I'm looking for help in how to properly modify my program so that a display message will indicate the number of incorrect guesses that remain each time the user enters an incorrect letter. This is for a program that simulates the Hangman game. Could anyone offer help or suggestions?


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

int main()
{
	//declare the variables
	string origWord = "";
	string letter = "";
	char dashReplaced ='N';
	char gameOver ='N';
	int numIncorrect = 0;
	string displayWord = "-----";

	//get original word
	do //begin loop
	{
		cout << "Enter a 5-letter word in uppercase: ";
		getline(cin, origWord);
	}   while (origWord.length() != 5);
	//clear the system
	system("cls");

	//start guessing
	cout << "Guess this word: " <<
	displayWord << endl;
	while (gameOver == 'N')
	{
		cout << "Enter an uppercase letter: ";
		cin >> letter;

		//serarch 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 replace, check whether the
		//displayWord variable contains any dashes
		if (dashReplaced == 'Y')
		{
			//if the displayWord variable does not
			//contain any dashes, 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

	system("pause");
	return 0;
} //end of main function  
Last edited on
Topic archived. No new replies allowed.