Can't find the logical error in UFO any help appreciated

Not sure the correct way to enter my code but here it goes.
Learn C++ | Codecademy
is my link to the code.
I must be too close to the problem but I cannot see what is wrong and it is probably something simple.

Thanks for any help

UFO.cpp
#include
#include “ufo_functions.hpp”
using namespace std;
int misses = 0;
string codeword = “codecademy”;
string answer = “__________”;

int main() {

char letter;
greet();
bool guess = false;
vector incorrect;

while (answer != codeword && misses < 7) {
display_misses(misses);
cout << “incorrect guesses:” << misses << “\n”;
display_status(incorrect, answer);
cout << “Please Enter your guess”;
cin >> letter;
cout << "Your guess = " << letter;
for (int i; i <= codeword.length(); i++) {

 if (codeword[i] == letter) {
   answer[i] = letter;
   cout << "Your guess = " << letter << "answer[i] = " << answer[i];
   guess = true;
 } 
 cout << "Your guess = " << letter << "answer[i] = " << answer[i];
 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++;
 }

}

// misses++;
}
end_game(codeword, answer);
}
ufo_functions.hpp
#include

void display_misses(int misses);
void end_game(std::string codeword, std::string answer);
void greet();
void display_status(std::vector incorrect, std::string answer);
ufo_functions.cpp
#include

#include

using namespace std;

// Define functions

void greet() {

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

std::cout << " UFO: The Game\n";

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

std::cout << “Instructions: save your friend from alien abduction\n”;

std::cout << “by guessing the letters in the codeword\n”;

}

void end_game(string code, string answer) {

if (answer == code) {

cout << "Hooray! You saved the player\n";

} else {

cout << "Oh no! the UFO just flew away\n";

}

}

void display_status(vector incorrect, string answer) {

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

for (int i = 0; i < incorrect.size(); i++) {

std::cout << incorrect[i] << ' ';

}

cout << “\nCodeword:\n” ;

for (int i; i <= answer.length(); i++) {

 cout << answer[i] << ' '; 

}

}

void display_misses(int misses) {

cout << "number of misses = " << misses << “\n”;

}

Welcome to the forums. Check the following if you want to see details about posting formatted code here-
How do I format code in my posts?

I think #include<string> and possibly include<vector> are missing from a couple of files where they’re used. The linker still needs to know where names are from, reusing headers is rarely an issue (for more complex .hpp files looking into header guards will likely be worthwhile in the future).

For finding your issue it might be worthwhile commenting out certain sections of your code to see what does and does not work. Most of it looks absolutely fine so it might be hard to find the exact sticking point without a little bit of trial and error (or perhaps a nicer compiler & IDE with some better warnings).

Thank you the #includes are there but didn’t copy across?
Header guards is a good idea thanks
I compiles but doesn’t go in to the loops and couts are not shown in to the loops or if thens but I can’t see why.
but thanks for the reply

1 Like

Ah, grand so yeah that points towards a possible issue. This line for example is problematic, can you see why (maybe compare it to some of the others)?
for (int i; i <= codeword.length(); i++)

If not:

Initialisation of i.

Think that might pop up more than once. It might not be the only issue but that would explain the loops failing to run at all.

Thank you I will look at that in the code. I think the second int i should be j. Well spotted. Hopefully, I will get a chance to implement it soon.

Solved int i was the key to solving all the errors. Thanks

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.