Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi there! I have this code in C++ and for some reason it produces these empty spaces between the lines of the letters still to

Hi there! I have this code in C++ and for some reason it produces these empty spaces between the lines of the letters still to be guessed? How do I fix it? So for example it prints out K _ _ _ _ _ instead of K_____

#include

#include

using namespace std;

const int MAX_GUESSES = 10;

// Prints empty lines at the beginning of execution

void clean_screen();

// Checks if all the letters include in the list of guessed letters

bool is_word_already_guessed(string secret, string guessed);

// Prints string using '_' in the places of letters not guessed

void print_game_status(string secret, string guessed);

int main() {

string secret_word = "";

cout

getline(cin, secret_word);

clean_screen();

string guessed_letters = "";

int guesses_used = 0;

while ( not is_word_already_guessed(secret_word, guessed_letters)

and guesses_used

cout

print_game_status(secret_word, guessed_letters);

if ( guessed_letters.size() > 0 ) {

cout

}

string guess = "";

cout

getline(cin, guess);

if ( guess.size() != 1 ) {

cout

continue;

} else if ( guessed_letters.find(guess) != string::npos ) {

cout

continue;

}

guessed_letters += guess;

if ( secret_word.find(guess) == string::npos ) {

cout

}

++guesses_used;

}

if ( not is_word_already_guessed(secret_word, guessed_letters) ) {

cout

cout

} else {

cout

cout

}

}

void clean_screen() {

// Cleaning screen by printing 100 empty lines.

for ( int i = 0; i

cout

}

}

bool is_word_already_guessed(string secret, string guessed) {

// Going through all the characters in secret string.

for ( string::size_type index = 0; index

// If such a character in secret string is met that is not in

// guessed letters, it can be immediately concluded that

// the whole secret word has not yet guessed.

if ( guessed.find(secret.at(index)) == string::npos ) {

return false;

}

}

// When all the secret string has been gone through, and each character

// was found from guessed string, it must hold that the whole

// secret word has been guessed.

return true;

}

void print_game_status(string secret, string guessed) {

// The characters of a string can be gone through also with such a for loop

// that looks like "for x in y" structure in Python.

// This is an alternative for the for loop with index variable

// used in previous function.

for ( char secret_char: secret ) {

if ( guessed.find(secret_char) == string::npos ) {

cout

} else {

cout

}

}

cout

}

image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2014 Nancy France September 15 19 2014 Proceedings Part I Lnai 8724

Authors: Toon Calders ,Floriana Esposito ,Eyke Hullermeier ,Rosa Meo

2014th Edition

3662448475, 978-3662448472

More Books

Students also viewed these Databases questions