Difficulty with special characters in hangman

Hi everyone, sorry for the late question
I have to submit a hangman and i have a problem when the word of the hangman has special characters, during the test the word is : #dnfé_t@xt # 1 _ @ i v f m a t and it's the only part that fails. Where does it come from ?
Thanks alot would help me so much. (same when some letters of this word are CAPS)
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
  #include "tests.h"

#include <iostream>
#include <string>
#include "hangman.h"
using namespace std;


int
main ()
{

  string word_to_guess = chooseWord ();
  int n_letters = word_to_guess.length ();
  string wordtoguess_bak = word_to_guess;
  for (int i = 0; i < n_letters; i++)
    {
      wordtoguess_bak.at (i) = '_';
    }

  cout <<
    "New Game. You are granted 6 incorrect guesses to find my word"
    << n_letters << " letters" << endl;

  int attempts = 6;

  char inchar;

  bool state = true;



  while (state)
    {
      cout << "Attempts left: " << attempts << endl << "[ ";
      for (int i = 0; i < n_letters; i++)
	{
	  cout << wordtoguess_bak.at (i) << " ";
	}
      cout << "]" << endl;

      cin >> inchar;
      cout << "Your guess: " << inchar << endl;

      string wordtoguess_bak_previous = wordtoguess_bak;
      for (int i = 0; i < n_letters; i++)
	{
	  if (inchar == word_to_guess.at (i))
	    {
	      wordtoguess_bak.at (i) = inchar;

	    }
	}
      if (wordtoguess_bak_previous == wordtoguess_bak)
	{
	  attempts -= 1;
	  drawHangman (attempts);
	}
      if (attempts <= 0)
	{
	  state = 0;
	  cout << endl << "The correct word was: " << word_to_guess << endl <<
	    "You lost!" << endl;
	}
      if (wordtoguess_bak == word_to_guess)
	{
	  cout << endl << "[ ";
	  for (int i = 0; i < n_letters; i++)
	    {
	      cout << wordtoguess_bak.at (i) << " ";
	    }
	  cout << "]" << endl << "You won!" << endl;
	  state = 0;
	}
    }



  return 0;
}
I won't attempt to go trough the logic of the code above, since you're including "hangman.h" so it may as well be coming from there :\

string word_to_guess = chooseWord ();

for what I know, the problem might as well be in some code you're not showing
Last edited on
Hi,
Thanks for your answer. Well the hangman.h was given by the tutor, we only had to include it and therefore should be correct. Same for tests.h wich is an automatic test from the Tutor where i got the error when he tested special character words. I looked through my code several times but cant find why it doesnt work properly as soon as you get special character combinations.
Thanks Marcus
it can't be helped if the situation of the fail is not clear.
What is the word being tested, and what is the input that fails?
ok, I can tell you the problem is with the 'é'.

If I input it in the console, what cin pass to char is -126 '‚' instead of -23 'é'

Is not much but is a starting point for further investigation
It works for me when I set word_to_guess to "apple" and remove the drawHangman call. Perhaps show us the implementation of drawHangman and chooseWord?
Yeah thanks that helped me to find where the problem occurs. In fact the test runs the code and compares if the output answer is what is expected. The problem is when the computer choses the word : #inf1_m@vt # 1 _ @ A B C D E F G or #inf1_m@vt # 1 _ @ i n f m v t
the code only detects a word of 10 characters beacuse 11th one is a blank. Therefore the computer finds the 10 character word and answers : You won ! Word was #inf1_m@vt because the end is not detected as part as the word.
it should detect the whole #inf1_m@vt # 1 _ @ i n f m v t as secret word and not only until the first blank. Anyways i cannot figure out how to make it read past the blank spaces.
And yes é is also a problem but it's written i should count accents and other ''unusual punctuation like ö etc... "
here is what i got about the function:
the chooseWord() function:
std::string word_to_guess = chooseWord();
which returns a random English word (from a predefined list) as a string. The latter is more fun since you can play against the computer this way.
Thanks alot
Last edited on
if you don't want to discard white-spaces, use this

cin.get(inchar);

cin has a flag that by default, tells it to discard whitespace. so if you want to continue using

cin >> inchar;

you need to unset said flag, so that cin won't skip whitespaces anymore.
As you can read here -> http://www.cplusplus.com/reference/ios/ios_base/
ios_base:
Base class for the entire hierarchy of stream classes in the standard input/output library, describing the most basic part of a stream which is common to all stream objects, independently of their character type.


as you can see in that page, there is a function "unsetf" thas is meant to unset a flag in a stream such as cin.
So you need to #include <iomanip> and call
cin.unsetf(ios::skipws);
so that cin won't discard whitespaces anymore. You can read the rest of the possible flags here:
http://www.cplusplus.com/reference/ios/ios_base/fmtflags/
Last edited on
Topic archived. No new replies allowed.