Lecture 21 - For loop challenge

Heya. Here’s my attempt at the for loop challenge in lecture 21:

#include <iostream>
#include <string>

void PrintIntro();
std::string GetGuess();
void PrintGuess(std::string Guess);

// the entry point for the application
int main() 
{
	constexpr int MAX_TRIES = 5;
	PrintIntro();

	for (int i = 0; i < MAX_TRIES; i++)
	{
	  PrintGuess(GetGuess());
	}

	return 0;
}

void PrintIntro()
{
	// introduce the game
	constexpr int WORD_LENGTH = 5;
	std::cout << "Welcome to Bulls and Cows, a fun word game." << std::endl;
	std::cout << "Can you guess the " << WORD_LENGTH;
	std::cout << " letter isogram I'm thinking of?" << std::endl;
	return;
}

std::string GetGuess()
{
	// get a guess from the player
	std::cout << "Enter your guess: ";
	std::string Guess = "";
	std::getline(std::cin, Guess);
	return Guess;
}

void PrintGuess(std::string Guess)
{
	// repeat the guess back to the player
	std::cout << "You guessed " << Guess << std::endl;
	return;
}
1 Like

How come you are still using std:: in your code? Wouldn’t it look cleaner to have using namespace std; and then not worry about doing it manually every time?

Heya Alunnite, thanks for the comment. While I agree it would be easier and look cleaner using the using namespace, I’ve read in the past that it’s not recommended as it can cause issues particularly in larger projects when you’re accessing multiple namespaces that may share duplicate method/function names. While not an issue for this project, I want to start getting into good habits from the start.

Fair enough, I didn’t know that some considered it bad practice. Actually once you reach the classes lesson in this course and start messing around with header files they actually recommend removing namespace. So your actually ahead of the curve on this one :sunglasses:

Lol yeah. Just got to the classes and headers tutorials tonight. Managed to save myself from doing a challenge :wink:

Privacy & Terms