Functions UFO Freeform Project

I followed the instructions of the project but I cannot seem to make the program run properly.https://gyazo.com/c9aeb50fed71bf4b613c6add62866e00

When I guess a character correctly the program still lists it under an incorrect guess.

This is my code:

ufo.cpp

#include “ufo.hpp”

int main() {

std::cout << “=============\n”;
std::cout << “UFO: The Game\n”;
std::cout << “=============\n”;
std::cout << “Instructions: save your friend from alien abduction by guessing the letters in the codeword.\n”;

while (answer != codeword && misses < 7) {

	take_turn(); 

}

if (answer == codeword) {

std::cout << "Hooray! You saved the person and earned a medal of honor!\n";

}

else {

std::cout << "Oh no! The UFO just flew away with another person!\n";

}

}

ufo.hpp

#include //#include iostream For some reason this is not showing up on this website
#include //#include vector For some reason this is not showing up on this website

void display(int misses);

// Declare variables
std::string codeword = “codecademy”;
std::string answer = “__________”;

int misses = 0;

std::vector incorrect;
bool guess = false;

// Define take_turn()
void take_turn() {

display(misses);

std::cout << “\nIncorrect Guesses:\n”;

for (int i = 0; i < incorrect.size(); i++) {
std::cout << incorrect[i] << ' ';
}

std::cout << “\nCodeword:\n”;

for (int i = 0; i < answer.length(); i++) {
std::cout << answer[i] << ’ ';
}

char letter;
std::cout << "\n\nPlease enter your guess: ";
std::cin >> letter;
for (int i = 0; i < codeword.length(); i++) {

if (letter == codeword[i]) {

	answer[i] = letter;
	guess = false;

}

}

if (guess) {

std::cout << "\nCorrect! You're closer to cracking the codeword.\n";

} 

else {

std::cout << "\nIncorrect! The tractor beam pulls the person in further.\n";
incorrect.push_back(letter);
	misses++;
}

}

void display(int misses) {

if (misses == 0 || misses == 1) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /     \\        (  Send help! ) \n";
std::cout << "             /   0   \\      / `-----------'  \n";
std::cout << "            /  --|--  \\    /                 \n";
std::cout << "           /     |     \\                     \n";
std::cout << "          /     / \\     \\                   \n";
std::cout << "         /               \\                   \n";

}
else if (misses == 2) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /  0  \\        (  Send help! ) \n";
std::cout << "             / --|-- \\      / `-----------'  \n";
std::cout << "            /    |    \\    /                 \n";
std::cout << "           /    / \\    \\                    \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}
else if (misses == 3) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /--|--\\        (  Send help! ) \n";
std::cout << "             /   |   \\      / `-----------'  \n";
std::cout << "            /   / \\   \\    /                \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}
else if (misses == 3) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /--|--\\        (  Send help! ) \n";
std::cout << "             /   |   \\      / `-----------'  \n";
std::cout << "            /   / \\   \\    /                \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}
else if (misses == 4) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /  |  \\        (  Send help! ) \n";
std::cout << "             /  / \\  \\      / `-----------' \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}
else if (misses == 5) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              / / \\ \\        (  Send help! )\n";
std::cout << "             /       \\      / `-----------'  \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}
else if (misses == 6) {

std::cout << "                 .                            \n";
std::cout << "                 |                            \n";
std::cout << "              .-\"^\"-.                       \n";
std::cout << "             /_....._\\                       \n";
std::cout << "         .-\"`         `\"-.                  \n";
std::cout << "        (  ooo  ooo  ooo  )                   \n";
std::cout << "         '-.,_________,.-'    ,-----------.   \n";
std::cout << "              /     \\        (  Send help! ) \n";
std::cout << "             /       \\      / `-----------'  \n";
std::cout << "            /         \\    /                 \n";
std::cout << "           /           \\                     \n";
std::cout << "          /             \\                    \n";
std::cout << "         /               \\                   \n";

}

}

Any help would be appreciated. It would be nice if codecademy posted an answer key for this project like they did for every other project.

Try this

#include <iostream>
#include <vector>

void display(int misses);

// Declare variables
int misses = 0;
std::string codeword = "codecademy";
std::string answer = "__________";
std::vector<char> incorrect;
bool guess = false;

// Define take_turn()
void take_turn()
{
	char letter;
	std::cout << "\n\nPlease enter your guess: ";
	std::cin >> letter;

	for (int i = 0; i < codeword.length(); i++)
	{
		if (letter == codeword[i])
		{
			answer[i] = letter;
			guess = true;
		}
	} 
	if (guess)
		std::cout << "Correct!";
	else 
	{
		std::cout << "Incorrect! The tractor beam pulls the person in further.\n";
		incorrect.push_back(letter);
		misses++;
	}
	guess = false;
	
	std::cout << "Incorrect Guesses:" << "\n";
	for (int i = 0; i < incorrect.size(); i++)
	{
		std::cout << incorrect[i] << ' ';
	}
	std::cout << "\nCodeword:\n";
	for (int i = 0; i < answer.size(); i++)
	{
		std::cout << answer[i] << ' ';
	}
	display(misses);
}
void display(int misses) {

	if (misses == 0 || misses == 1) {

		std::cout << "                 .                            \n";
		std::cout << "                 |                            \n";
		std::cout << "              .-\"^\"-.                       \n";
		std::cout << "             /_....._\\                       \n";
		std::cout << "         .-\"`         `\"-.                  \n";
		std::cout << "        (  ooo  ooo  ooo  )                   \n";
		std::cout << "         '-.,_________,.-'    ,-----------.   \n";
		std::cout << "              /     \\        (  Send help! ) \n";
		std::cout << "             /   0   \\      / `-----------'  \n";
		std::cout << "            /  --|--  \\    /                 \n";
		std::cout << "           /     |     \\                     \n";
		std::cout << "          /     / \\     \\                   \n";
		std::cout << "         /               \\                   \n";

	}
	else if (misses == 2) {

		std::cout << "                 .                            \n";
		std::cout << "                 |                            \n";
		std::cout << "              .-\"^\"-.                       \n";
		std::cout << "             /_....._\\                       \n";
		std::cout << "         .-\"`         `\"-.                  \n";
		std::cout << "        (  ooo  ooo  ooo  )                   \n";
		std::cout << "         '-.,_________,.-'    ,-----------.   \n";
		std::cout << "              /  0  \\        (  Send help! ) \n";
		std::cout << "             / --|-- \\      / `-----------'  \n";
		std::cout << "            /    |    \\    /                 \n";
		std::cout << "           /    / \\    \\                    \n";
		std::cout << "          /             \\                    \n";
		std::cout << "         /               \\                   \n";

	}
	else if (misses == 3) {

		std::cout << "                 .                            \n";
		std::cout << "                 |                            \n";
		std::cout << "              .-\"^\"-.                       \n";
		std::cout << "             /_....._\\                       \n";
		std::cout << "         .-\"`         `\"-.                  \n";
		std::cout << "        (  ooo  ooo  ooo  )                   \n";
		std::cout << "         '-.,_________,.-'    ,-----------.   \n";
		std::cout << "              /--|--\\        (  Send help! ) \n";
		std::cout << "             /   |   \\      / `-----------'  \n";
		std::cout << "            /   / \\   \\    /                \n";
		std::cout << "           /           \\                     \n";
		std::cout << "          /             \\                    \n";
		std::cout << "         /               \\                   \n";

	}
	else if (misses == 3) {

		std::cout << "                 .                            \n";
		std::cout << "                 |                            \n";
		std::cout << "              .-\"^\"-.                       \n";
		std::cout << "             /_....._\\                       \n";
		std::cout << "         .-\"`         `\"-.                  \n";
		std::cout << "        (  ooo  ooo  ooo  )                   \n";
		std::cout << "         '-.,_________,.-'    ,-----------.   \n";
		std::cout << "              /--|--\\        (  Send help! ) \n";
		std::cout << "             /   |   \\      / `-----------'  \n";
		std::cout << "            /   / \\   \\    /                \n";
		std::cout << "           /           \\                     \n";
		std::cout << "          /             \\                    \n";
		std::cout << "         /               \\                   \n";

	}
	else if (misses == 4) {

		std::cout << "                 .                            \n";
		std::cout << "                 |                            \n";
		std::cout << "              .-\"^\"-.                       \n";
		std::cout << "             /_....._\\                       \n";
		std::cout << "         .-\"`         `\"-.                  \n";
		std::cout << "        (  ooo  ooo  ooo  )                   \n";
		std::cout << "         '-.,_________,.-'    ,-----------.   \n";
		std::cout << "              /  |  \\        (  Send help! ) \n";
		std::cout << "             /  / \\  \\      / `-----------' \n";
		std::cout << "            /         \\    /                 \n";
		std::cout << "           /           \\                     \n";
		std::cout << "          /             \\                    \n";
		std::cout << "         /               \\                   \n";

	}
	else if (misses == 5) {

		std::cout << "                 .                            \n";
		std::cout << "                 |                            \n";
		std::cout << "              .-\"^\"-.                       \n";
		std::cout << "             /_....._\\                       \n";
		std::cout << "         .-\"`         `\"-.                  \n";
		std::cout << "        (  ooo  ooo  ooo  )                   \n";
		std::cout << "         '-.,_________,.-'    ,-----------.   \n";
		std::cout << "              / / \\ \\        (  Send help! )\n";
		std::cout << "             /       \\      / `-----------'  \n";
		std::cout << "            /         \\    /                 \n";
		std::cout << "           /           \\                     \n";
		std::cout << "          /             \\                    \n";
		std::cout << "         /               \\                   \n";

	}
	else if (misses == 6) {

		std::cout << "                 .                            \n";
		std::cout << "                 |                            \n";
		std::cout << "              .-\"^\"-.                       \n";
		std::cout << "             /_....._\\                       \n";
		std::cout << "         .-\"`         `\"-.                  \n";
		std::cout << "        (  ooo  ooo  ooo  )                   \n";
		std::cout << "         '-.,_________,.-'    ,-----------.   \n";
		std::cout << "              /     \\        (  Send help! ) \n";
		std::cout << "             /       \\      / `-----------'  \n";
		std::cout << "            /         \\    /                 \n";
		std::cout << "           /           \\                     \n";
		std::cout << "          /             \\                    \n";
		std::cout << "         /               \\                   \n";

	}

}
1 Like

That works, thank you. I see what I did wrong.

1 Like