hangman game (sloppy code warning im new)

ok so the thing is that i have been working on a hangman game and its going good besides one problem i cannot figure out how to show all the letters that have been used on the screen at all times could i get some help??? (by the way it has to be in a function which i dont totally understand either )

#include <ctime>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;





int main(){



string s1;
char guess;
string dash;
int bla = 0;
int x = 0;
int b = 0;

cout << "Let's play hangman.. all you have to do is type in a word and have player 2" << endl << "guess your word but you can only be wrong 6 times" << endl;
getline(cin, s1);
system("cls");
cout << "Guess what the word is one letter at a time :-)" << endl;
dash = s1;
for (int z = 0; z < s1.length(); z++){ dash[z] = '-'; cout << dash[z]; }
cout << endl;


while (b < 6){
cin >> guess;
for (int x = 0; x < s1.length(); x++){
if (guess == s1[x]){ system("cls"); dash[x] = guess; cout << dash << endl; }
else{ system("cls"); bla = bla + 1; cout << dash << endl; }
if (bla == s1.length()){ system("cls"); b = b + 1; cout << " you have: " << 6 - b << " tries left" << endl; }
cout << endl << endl;

}
bla = 0;
}







system("pause");
return 0;
}
Last edited on
Use a vector. Also put code tags on your code.
Just for fun...
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 <limits>
#include <cctype>

using namespace std;

int main()
{
	cout << "Enter secret word" << endl;
	string secret;
	cin >> secret;
	for (char &c : secret) c = toupper(c);
	cout << string(1024, '\n');
	string guess(secret.size(), '_');
	constexpr size_t guesses {6};
	cout << "You have " << guesses << " guesses." << endl;
	cout << "Guess a letter, or '*' to guess whole word" << endl;
	cout << guess << endl;
	char letter = 0;
	for (size_t i = 0; i < guesses; ++i)
	{
		do
		{
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n' );
			cout << "guess " << i + 1 << "?" << endl;
			letter = cin.get();
		}
		while (!cin || ('*' != letter && !isalpha(letter)));

		if ('*' == letter)
		{
			cout << "Word? "; 
			string word;
			cin >> word;
			for (char &c : word) c = toupper(c);
			if (word == secret)
			{
				guess = secret;
			}
			
			break;
		}
		
		for (size_t j = 0; j < guess.size(); ++j)
		{
			letter = toupper(letter);
			if (secret[j] == letter)
			{
				guess[j] = letter;
			}
		}
		
		cout << guess << endl;
		if (secret == guess)
		{
			break;
		}
	}
	
	if (secret == guess)
	{
		cout << "You win!" << endl;
	}
	else
	{
		cout << "Tough luck, the word was\n" << secret << endl;
	}
}
When I the code above, it got 20 errors.
Which of the blocks of code are you referring to, OP or mine?
What type of errors - compilation errors?
What are the errors?
What dialect of C++ did you set your compiler to (-std=c++11)?
Topic archived. No new replies allowed.