Im trying to get this game to play again once the user gets the right answer or the guesses exceede 8. The problem comes in when i type in Y or N. It doesnt loop back up to the top of Do loop like i would like it too. Please help.

#include <iostream>
#include <ctime> // For time()
#include <cstdlib> // For srand() and rand()

using namespace std;

void promptUser(char guess[], int n);
int *getRandomNumbers(int);
bool endGame(char guess[], char answer[]);
char setColor(int num);

int main()
{
	char guess[4];
	char answer[4];
	bool playAgain = true;
	bool success;
	bool win;
	int guesses;
	char vote;
	int *numbers;

	


	do
	{

		success = false;
		guesses = 8;

		cout << endl;
		numbers = getRandomNumbers(4);

		for(int count = 0; count < 4; count++)
		{
			cout << numbers[count] << endl;
			answer[count] = setColor(numbers[count]);
			cout << answer[count] << endl;

		}
		cout << endl;
	
		

		while (!success && guesses > 0)
		{
			promptUser(guess, guesses);
			success = endGame(guess, answer);
			guesses--;
		}

		
		if(success)
		{
			cout << "You guessed the correct answer" << endl;
			cout << "The correct answer was: " << answer[0] << answer[1] << answer[2] << answer[3] << endl;


		}
		else
		{
			cout << "Sorry!!! The correct answers were: " << answer[0] << answer[1] << answer[2] << answer[3] << endl;

		}

		cout << "Play agains? (Y or N): ";
		cin >> playAgain;

		

	}while( (playAgain == 'Y' || playAgain == 'y' ));


	return 0;

}

int *getRandomNumbers(int num)
{

	int *arr;
	

	if (num <= 0)
		return NULL;

	arr = new int[num];

	srand((unsigned int) time(0) );
	
	for (int count = 0; count < num; count++)
	{
		arr[count] = (rand()% 100) + 1;
	}

	return arr;
}

void promptUser(char guess[], int n)
{
	cout << "Please enter a guess: ";

	cin >> guess[0] >> guess[1] >> guess [2] >> guess[3];

	cout << guess[0] << endl;
	cout << guess[1] << endl;
	cout << guess[2] << endl;
	cout << guess[3] << endl;


}

char setColor(int num)
{
	if (num > 0 && num < 18)
	{
		return static_cast<char>(82);
	}
	else if ( num >= 18 && num < 35)
	{
		return static_cast<char>(79);
	}
	else if ( num >= 35 && num < 52)
	{
		return static_cast<char>(89);
	}
	else if ( num >= 52 && num < 69)
	{
		return static_cast<char>(71);
	}
	else if ( num >= 69 && num < 86)
	{
		return static_cast<char>(66);
	}
	else if ( num >= 86 && num <= 100)
	{
		return static_cast<char>(86);
	}

}

bool endGame(char guess[], char answer[])
{
	int totalCorrect = 0;
	int correctColor = 0;
	bool status = false;

	if(guess[0] == answer[1] || guess [0] == answer [2] || guess [0] == answer[3])
	{
		// insert an x
		totalCorrect++;

	}
	else
	{
		//insert a dash
	}
	if(guess[1] == answer[0] || guess [1] == answer [2] || guess [1] == answer[3])
	{
		// insert an x
		totalCorrect++;

	}
	else
	{
		//insert a dash
	}
	if(guess[2] == answer[1] || guess [2] == answer [0] || guess [2] == answer[3])
	{
		// insert an x
		totalCorrect++;

	}
	else
	{
		//insert a dash
	}
	if(guess[3] == answer[1] || guess [3] == answer [2] || guess [3] == answer[0])
	{
		// insert an x
		totalCorrect++;

	}
	else
	{
		//insert a dash
	}
	
	for(int i = 0; i < 4; i++)
	{

		if(guess[i] == answer[i])
		correctColor++;

	}
	if (correctColor == 4)
	{
		status = true;

	}
	

	cout << correctColor;
	cout << totalCorrect;

	return status;
}

Recommended Answers

All 2 Replies

bool playAgain = true;
cin >> playAgain;
while( (playAgain == 'Y' || playAgain == 'y' ));

Enough of a hint? :)

GAH im an idiot sometimes....thx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.