I have a written a fully functional hangman game in C++ (my first project). Everything works just fine but the output in the terminal of VS code has an issue. The steps to hang the man are being written on top of eachother, even though I use a function that clears the output screen every time the user inputs a new character. It doesn't clean the whole screen apparently. I used the same code on replit, where the screen was cleaned perfectly without mixing the steps to hang the man. See it yourself:
And that's the code:
#include <iostream>
#include <string>
#include <vector>
#include <ctime> // Required for srand() //
#include <unistd.h> // Required for sleep() //
#include <stdlib.h> // Required for clearing the screen //
#include <cctype> // Required for isdigit() //
using namespace std;
void display_console (int, string, string, vector <char>);
string assigning_secret_word ();
string assigning_guess_word (string);
void update_console (int &, string &, string, vector <char>&, char);
void update_game (int, string, string, char);
int main() {
char selection;
char character;
do {
int increment{0};
vector <char> wrong_characters;
srand(time(NULL));
string secret_word = assigning_secret_word ();
string guess_word = assigning_guess_word(secret_word);
cout << endl;
cout << "Press ""0"" to start a new game" <<endl;
cout << "Press ""1"" to end game -> ";
cin >> selection;
cout << "\033[2J\033[1;1H";
switch (selection) {
case '0':
while (increment < 9 && secret_word!= guess_word) {
display_console(increment, secret_word, guess_word, wrong_characters);
cout << "Enter an english character: ";
cin >> character;
if (character == '9') {
break; }
else if (isdigit(character) == 1){
cout << "\033[2J\033[1;1H";
cout << "Invalid input. Please try again: ";
cin >> character;
character = toupper(character);
}
else { cout << "\033[2J\033[1;1H";
character = toupper(character); }
update_console (increment, guess_word, secret_word, wrong_characters, character); }
update_game (increment, guess_word, secret_word, selection);
cout << "\033[2J\033[1;1H";
break;
case '1':
return 0;
break;
default:
cout << "Invalid input. Please try again: ";
cout << endl;
sleep(3);
cout << "\033[2J\033[1;1H";
continue; }} while (selection != '1');
return 0;}
void display_console (int increment, string secret_word, string guess_word, vector <char> wrong_characters) {
string console [10] {
"",
"| \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n",
"|------- \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n",
"|-------| \n"
"| | \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n",
"|-------| \n"
"| | \n"
"| O \n"
"| \n"
"| \n"
"| \n"
"| \n"
"| \n",
"|-------| \n"
"| | \n"
"| O \n"
"| | \n"
"| \n"
"| \n"
"| \n"
"| \n",
"|-------| \n"
"| | \n"
"| O \n"
"| /| \n"
"| \n"
"| \n"
"| \n"
"| \n",
"|-------| \n"
"| | \n"
"| O \n"
"| /|\\ \n"
"| \n"
"| \n"
"| \n"
"| \n",
"|-------| \n"
"| | \n"
"| O \n"
"| /|\\ \n"
"| | \n"
"| / \n"
"| \n"
"| \n",
"|-------| \n"
"| | \n"
"| O \n"
"| /|\\ \n"
"| | \n"
"| / \\ \n"
"| \n"
"| \n"
};
cout << console[increment] << endl;
cout << guess_word << endl;
cout << endl;
cout << "*Press '9' to go back to the menu*" << endl;
cout << endl;
cout << "Wrong characters: {";
for (auto temps: wrong_characters) {
cout << temps << ", ";
}
cout << "}" << endl; }
string assigning_secret_word () {
string words [10] {"MERCEDES", "TURKEY", "UNIVERSITY", "MICHAEL", "GARLIC", "SPORTS", "AIRPLANE", "RESTAURANT", "TELEVISION", "ADVERTISEMENT"};
int num = rand()%10;
return words[num];
}
string assigning_guess_word (string secret_word) {
string guess_word;
for (int i{0}; i < secret_word.length(); i++) {
guess_word.push_back('_');
}
return guess_word; }
void update_console (int &increment, string &guess_word, string secret_word, vector <char> &wrong_characters, char character) {
bool winning_character = false;
for (int j{0}; j < secret_word.size(); j++) {
if(j < guess_word.length() && (guess_word[j] == character || (j < wrong_characters.size() && wrong_characters[j] == character))) {
cout << "This character has already been opened. Please try again: ";
cin >> character;
cout << "\033[2J\033[1;1H";
character = toupper(character); }
else if (secret_word[j] == character) {
guess_word[j] = character;
winning_character = true;
}
else if (secret_word[j] != character) {
if (j == (secret_word.size()-1) && winning_character == false) {
wrong_characters.push_back(character);
int minDistance = INT_MAX;
char nearestChar = '\0';
for (int i=0; i<secret_word.size(); i++) {
if (guess_word[i] == '_') {
int distance = abs(character - secret_word[i]);
if(distance < minDistance) {
minDistance = distance;
nearestChar = secret_word[i]; }
}
else {
continue;
}}
int table = abs(int(character) - int(nearestChar));
if (table >= 1 && table <=5) {
increment = increment +1;
}
if (table >= 6 && table <=10) {
increment = increment +2;
}
if (table >= 11 && table <=15) {
increment = increment +3;
}
if (table >= 16 && table <=20) {
increment = increment +4;
}
if (table >= 21 && table <=25) {
increment = increment +5;
}
}
}
cout << "\033[2J\033[1;1H";}}
void update_game (int increment, string guess_word, string secret_word, char selection) {
if (increment >= 10) {
cout << "----------------------------------------------" << endl;
cout << "You have lost. Better luck next time!" << endl;
cout << "----------------------------------------------" << endl;
sleep(3);
}
if (secret_word == guess_word) {
cout << "----------------------------------------------" << endl;
cout << "Congratulations! You have won!" << endl;
cout << "----------------------------------------------" << endl;
sleep(3);
}
cout << "\033[2J\033[1;1H";
}
I didn't try anything since I have no clue what the problem could be. One thing I am sure of is that the code has no errors because it worked on both VS Code and replit.