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? #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 << "Give a word to be guessed: "; 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 < MAX_GUESSES ) { cout << endl << "Game status: "; print_game_status(secret_word, guessed_letters); if ( guessed_letters.size() > 0 ) { cout << "You have used " << guesses_used << " guesses!"<< endl << "You have guessed letters: " << guessed_letters << endl; } string guess = ""; cout << "Guess a letter: "; getline(cin, guess); if ( guess.size() != 1 ) { cout << "You failed: give exactly one letter!" << endl; continue; } else if ( guessed_letters.find(guess) != string::npos ) { cout << "You failed: you had already guessed the letter " << guess << "!" << endl; continue; } guessed_letters += guess; if ( secret_word.find(guess) == string::npos ) { cout << "Guessed letter does not include the word!" << endl; } ++guesses_used; } if ( not is_word_already_guessed(secret_word, guessed_letters) ) { cout << endl << "Guesses expired!" << endl; cout << "The correct word was: " << secret_word << endl; } else { cout << endl << "Right guess. You used " << guesses_used << " guesses!" << endl; cout << "The correct word was: " << secret_word << endl; } } void clean_screen() { // Cleaning screen by printing 100 empty lines. for ( int i = 0; i < 100; ++i ) { cout << endl; } } bool is_word_already_guessed(string secret, string guessed) { // Going through all the characters in secret string. for ( string::size_type index = 0; index < secret.size(); ++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 << secret_char;
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
Please send me answer within 10 min!! I will rate you good for sure!!
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?
#include <iostream>
#include <string>
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 << "Give a word to be guessed: ";
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 < MAX_GUESSES ) {
cout << endl
<< "Game status: ";
print_game_status(secret_word, guessed_letters);
if ( guessed_letters.size() > 0 ) {
cout << "You have used " << guesses_used << " guesses!"<< endl << "You have guessed letters: " << guessed_letters << endl;
}
string guess = "";
cout << "Guess a letter: ";
getline(cin, guess);
if ( guess.size() != 1 ) {
cout << "You failed: give exactly one letter!" << endl;
continue;
} else if ( guessed_letters.find(guess) != string::npos ) {
cout << "You failed: you had already guessed the letter " << guess << "!" << endl;
continue;
}
guessed_letters += guess;
if ( secret_word.find(guess) == string::npos ) {
cout << "Guessed letter does not include the word!" << endl;
}
++guesses_used;
}
if ( not is_word_already_guessed(secret_word, guessed_letters) ) {
cout << endl << "Guesses expired!" << endl;
cout << "The correct word was: " << secret_word << endl;
} else {
cout << endl << "Right guess. You used " << guesses_used << " guesses!" << endl;
cout << "The correct word was: " << secret_word << endl;
}
}
void clean_screen() {
// Cleaning screen by printing 100 empty lines.
for ( int i = 0; i < 100; ++i ) {
cout << endl;
}
}
bool is_word_already_guessed(string secret, string guessed) {
// Going through all the characters in secret string.
for ( string::size_type index = 0; index < secret.size(); ++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 << secret_char;
}
}
cout << endl;
}
Step by step
Solved in 2 steps