Console closes regardless

I’m not sure if it is my VS or not because I have tried fixing and comparing my code exactly to the one in the video and then doing the same by taking the GitHub clone and running it and end up with the same results.
I end up in the picture where it will close out the console. I already looked to see if I have the properties set up correctly which I do, to have console selected under system and I am running test without debugging and I have tried with ctrl + f5.

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
bool AskToPlayAgain();

// the entry point for our application
int main()
{
	PrintIntro();
	PlayGame();
	AskToPlayAgain();
	return 0; // exit the application
}


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

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

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

bool AskToPlayAgain()
{
	cout << "Do you want to play again? ";
	string Response = "";
	getline(cin, Response);
	return (Response[0] == 'y') || (Response[0] == 'Y');
}

One solution is to put
system(“Pause”);

instead of
return 0;

in your int main ()

I gave that a try and I’m still running into this.
The console closes right after.

Not really sure why this isn’t helping, since system(“PAUSE”); should really work.

Another (rather silly) workaround is to put this at the end of the code (before return 0;)

int a = 0;
cin >> a;

Then
return 0;

This will keep your console opened, just because it will wait for your input :stuck_out_tongue:

If you can copy me your code or link me your git with code uploaded.
Regards,

urška

Privacy & Terms