How to save unknown amount of letters to the same variable w/ headerfile

Hi,
I am trying to make a hangman game and want to use a header file for the basic logic, and the main file simply for "ui"(command prompt). I want to save the guesses a user inputs, so if the user firsts inputs A, it gets saved in a variable, and then later if the user enters X it gets stored in the same variable. I have tried vectors, maps basically everything for 4/5 hours now and just cannot figure this out. I can save it to the variable, but every time I want to add something new it seems to reset what is in there. Any help would be appreciated!

What am I trying to save?
-char.
How?
-In one variable (I have no clue which one as they all seem to not work.
For What?
-Displaying to the users and for checking if the user enters the same letter 2 times.
What is the input?
A capital letter A-Z, letters have been filtered for errors before.
Please show your attempts so we can tell what you did wrong.
Sorry for the naming, but here is the header file's variables that have to do with this:
1
2
std::map<char, bool> LetterSeen;
	char Test[26]{ { 'A' },{ 'B' },{ 'C' },{ 'D' },{ 'E' },{ 'F' },{ 'G' },{ 'H' },{ 'I' },{ 'J' },{ 'K' },{ 'L' },{ 'M' },{ 'N' },{ 'O' },{ 'P' },{ 'Q' },{ 'R' },{ 'S' },{ 'T' },{ 'U' },{ 'V' },{ 'W' },{ 'X' },{ 'Y' },{ 'Z' }};

Printing out in main:
1
2
3
4
5
6
for (int i = 26; i >= 0; i--) {
			char Letter = HangManGame.Test[i];
			if (HangManGame.LetterSeen[Letter]) {
				std::cout << Letter << ", ";
			}
		}

and this is the thing putting it in:
1
2
3
4
5
GEuessCheckStatus HangMan::SubmitGuess(std::string Guess)
{
	LetterSeen[Guess[0]] = true;
	return GEuessCheckStatus();
}

The return type is made for a feature that I still need to implement. The guess coming in is a single capital letter (I know it is the correct thing, so I wont bore you with more code)
My other attempts I lost, sorry
Last edited on
The operator[] of map always creates an entry. You don't need map. The std::set is sufficient.

If guess is in the set, then it is in the set.
See http://www.cplusplus.com/reference/set/set/count/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::set<char> LetterSeen;

bool newguess( char guess ) {
  if ( 0 < LetterSeen.count( guess ) ) // already tried
    return false;
  else {
    LetterSeen.insert( guess );
    return /* Is the guess correct? */ ;
  }
}


// print guesses
for ( auto Letter : LetterSeen ) {
  std::cout << Letter << ", ";
}



String works too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string LetterSeen;

bool newguess( char guess ) {
  if ( std::string::npos != LetterSeen.find(guess) ) // already tried
    return false;
  else {
    LetterSeen.append( guess );
    // you could sort the guesses
    return /* Is the guess correct? */ ;
  }
}


// print guesses
for ( auto Letter : LetterSeen ) {
  std::cout << Letter << ", ";
}

Hi,
Thanks sooo much that you replied. Unfortionately it did not work. I have made a little screencapture showing what I observe here (using the string, as it seemed to work best):
https://www.youtube.com/watch?v=Csxk1KwR5u0&feature=youtu.be
And this is my "full" code:
Main File: http://hastebin.com/hihusamopu.cpp
Header File: http://hastebin.com/zifilusegi.h
Headers Implementation: http://hastebin.com/ilomixeqem.cpp

Thanks again for replying :) i think I have by now spent around 6 hours trying :P
One of the problems is that you create a fresh instance of the hangman class on each repetition of the loop. You also have a global variable which should be encapsulated within the class. I didn't look at any of the other parts of the code.
Hi,
Thanks for pointing out that I am making that fresh intance every singly time, sooooooooooooo stupid :/ As for the public variable consirned, I was just keeping it open to keep the sectors where it could go wrong near to none as I would just directly reference it. Now it is solved I shalt create a getter class for it! Thanks sooooo much for all your help!
Topic archived. No new replies allowed.