My if statement for the 5 loops of getGuess()

Dont know why I have errors in my code

#include
#include
using namespace std;
void PrintIntro();
string GetGuessAndPrintBack();
// Main is the entry point for our application.
int main() {
PrintIntro();
// Loop for the number of turns asking for guesses
constexpr int NUMBER_OF_TURNS = 5;
for (int count = 1; count <= NUMBER_OF_TURNS; count++)
{
GetGuessAndPrintBack();
cout << endl;
}
return 0;
}
void PrintIntro() {
constexpr int WORD_LENGTH = 9;
// Introduce the game.
cout << “Welcome to Bulls and Cows. a fun word game.\n\n”;
cout << “Can you guess the " << WORD_LENGTH;
cout << " letters I’m thinking of?\n\n”;
cout << endl;
return;
}
// GetGuessAndPrintBack Method
string GetGuessAndPrintBack() {
// Get a guess from the player.
cout << "Enter your guess here: ";
string Guess = “”;
getline(cin, Guess);
// Repeat the guess back to the player.
cout << "Your guess was: " << Guess << endl;
cout << endl;
}

One issue I can see is your GetGuessAndPrintBack method has a return type of string but you never actually return anything at the end, adding a line with return Guess at the end of the method should fix that. A tip for next time - it’s hard to debug someone’s code with vague statements like “I have errors,” a screenshot or paste of the actual errors you’re getting will get you a more helpful answer much faster. Also, when you post pasted code, go back and highlight it and hit the “Preformatted text” button, makes it much more readable.

Thank you for the tip, and I will try that you next time. I didn’t know that methods you mentioned existed.

It worked, Thank you soooooo much.

        #include<iostream>
    	#include<string>
    	using namespace std;
    	void PrintIntro();
    	string GetGuessAndPrintBack();
    	// Main is the entry point for our application.
    	int main() {
    	PrintIntro();
    	// Loop for the number of turns asking for guesses
    	constexpr int NUMBER_OF_TURNS = 5;
    	for (int count = 1; count <= NUMBER_OF_TURNS; count++)
    	{
    		GetGuessAndPrintBack();
    		cout << endl;
    	}
    	return 0;
    }
    	void PrintIntro() {
    	constexpr int WORD_LENGTH = 9;
    	// Introduce the game.
    	cout << "Welcome to Bulls and Cows. a fun word game.\n\n";
    	cout << "Can you guess the " << WORD_LENGTH;
    	cout << " letters I'm thinking of?\n\n";
    	cout << endl;
    	return;
    }
    	// GetGuessAndPrintBack Method
    	string GetGuessAndPrintBack() {
    	// Get a guess from the player.
    	cout << "Enter your guess here: ";
    	string Guess = "";
    	getline(cin, Guess);
    	// Repeat the guess back to the player.
    	cout << "Your guess was: " << Guess << endl;
    	cout << endl;
    	return Guess;
    }
1 Like

Privacy & Terms