Skip to content
Advertisement

The first character in the output stream is replaced with the last character of that stream which is removed. Why is that so?

Program’s Aim:

The program is a hangman game which get a list of planets from our solar system, saves it to an array then randomly selects one word from the array and subsequently prints two letters of the word on the board. The program runs perfectly in a windows environment, but fails in runtime on linux.

Problem:

The program replaces the first character in the output stream with the last character of that stream. That happens when printing the word on the board and also when the word is displayed when it is correctly or wrongly guessed.

e.g.: word = Mars

For 6 wrong guess it’s supposed to print, Too bad you didn’t guess right. It was “mars”.

Instead, it prints: “oo bad you didn’t guess right. It was “mars

Here are the files:

wordlist.yx:

JavaScript

Hangman.h:

JavaScript

Hangman.cpp:

JavaScript

main.cpp:

JavaScript

Tried:

  1. Running the program in a lower standard (11) from 14.
  2. Online compiler from tutorialspoint.
  3. Manually setting the word.

Observation:

I observed that if I manually set the word, that situation is resolved. How do I narrow in on this issue further?

Edit:

Added wordlist and removed a stray backslash which prevented compiling..

Advertisement

Answer

Try running dos2unix on your wordlist.yx file. If it was created on Windows, the newlines are probably rn, not just n. And if that r is interpreted literally, that will return to the beginning of the line prior to continuing to output to cout, thus overwriting your first character.

Reference to dos2unix can be found here.

To avoid using dos2unix, you could strip the r characters from your wordlist array. One approach would be to loop through each word in the array and processing them like so:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement