Question about my code

So I attempted to recreate the code so far without using the std namespace, but when I run the program it only asks the question once and does not continue to loop the question. I compared it to the separate code I had that was identical to Ben’s but I can’t seem to figure out the issue. This is my first post so my apologies if the code is not formatted well.

#include <iostream>
#include <string>



void PrintIntro();
void PlayGame();
std::string GetGuess();


//intro to application
int main()
{
	PrintIntro();
	PlayGame();
	return 0;
}


//introduction to player
void PrintIntro() {
	constexpr int WORD_LENGTH = 5;
	std::cout << "Welcome to Bulls and Cows a Fun Word Game.\n";
	std::cout << "Can you guess the " << WORD_LENGTH << " letter isogram that i'm thinking of?\n";
	std::cout << std::endl;
	return;
}




//loop the number of turns asking for guesses
void PlayGame() {
	constexpr int NUMBER_OF_TURNS = 5;
	for (int count = 1; count <= NUMBER_OF_TURNS; count++); {
		std::string Guess = GetGuess();
		std::cout << "Your guess was: " << Guess << std::endl;
		std::cout << std::endl;
	}
}

//get player input
std::string GetGuess() {
	std::string Guess = "";
	std::cout << "What is your guess?: ";
	std::getline(std::cin, Guess);
	return Guess;
}

You have a semicolon here. Which means the code you wrote is equivalent to this

void PlayGame() {
	constexpr int NUMBER_OF_TURNS = 5;
	for (int count = 1; count <= NUMBER_OF_TURNS; count++)
    {
        //do nothing 5 times
    }
    {
		std::string Guess = GetGuess();
		std::cout << "Your guess was: " << Guess << std::endl;
		std::cout << std::endl;
	}
}

Oh wow I can’t believe I didn’t notice something so simple. I need to pay more attention to small details like that but thank you for the assistance it’s much appreciated. :smiley:

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

Privacy & Terms