Question: Need help for my c++ bulls and cows game input validation. PLEASE! When I type a word that is more than 4 characters or digits

Need help for my c++ bulls and cows game input validation. PLEASE!

When I type a word that is more than 4 characters or digits while either 'Setting a puzzle' or 'Solving a puzzle' the program crashes. How do I avoid that? I need to have the user try inputting a word that's 4 characters or digits only and if they don't, I need to loop them until they do so without affecting the number of attempts they have to play which is about 5 attempts.

#include

#include

using namespace std;

//global score counter

int cows = 0, bulls = 0;

// structure

struct number

{

char a, b, c, d;

};

//Defined before main. These are the 3 functions used in this program

void countScore(number X, number Y) // where x is bulls and y is cows

{

if (X.a == Y.a)

bulls += 1;

else if ((X.a == Y.b) || (X.a == Y.c) || (X.a == Y.d))

cows += 1;

if (X.b == Y.b)

bulls += 1;

else if ((X.b == Y.a) || (X.b == Y.c) || (X.b == Y.d))

cows += 1;

if (X.c == Y.c)

bulls += 1;

else if ((X.c == Y.b) || (X.c == Y.b) || (X.c == Y.d))

cows += 1;

if (X.d == Y.d)

bulls += 1;

else if ((X.d == Y.a) || (X.d == Y.b) || (X.d == Y.c))

cows += 1;

}

//This is a function to set puzzle and store it in a file

void setPuzzle()

{

number puzz;

cout << " Enter numbers or characters of a word to set as puzzle: ";

cin >> puzz.a >> puzz.b >> puzz.c >> puzz.d;

//output file and open

ofstream file;

file.open("secret.txt");

file << puzz.a << puzz.b << puzz.c << puzz.d;

cout << " Set Succesfully ";

//close file

file.close();

}

//This is a function to solve puzzle that is stored in the file

void solvePuzzle()

{

number guess, puzz;

cout << " Player, Enter your 4 digits or character guess : ";

cin >> guess.a >> guess.b >> guess.c >> guess.d;

// input file and open

ifstream file;

file.open("secret.txt");

file >> puzz.a >> puzz.b >> puzz.c >> puzz.d;

countScore(puzz, guess);

//closefile

file.close();

}

//Bool to prompt the user if they want to start over the again. If no then it exits before letting it loop

bool doItAgain();

bool validateYesOrNo(char);

int main()

{

int choice, solved = 0;

//Introduce

cout << "\t\t\tWelcome to Cows and Bulls - Game" << endl << endl;

//Instructions

cout << "================================= How To Play ================================= ";

cout << "[*] The player has the option to either write or guess a 4-digit numbers or\t";

cout << " characters that will be written to a secret.txt file ";

cout << "[*] If the player guesses a correct digit/character in the correct position,\t";

cout << " it is called a \"BULL\" ";

cout << "[*] If the player matches a digit/character but in the wrong position,\t\t";

cout << " it is called a \"COW\" ";

cout << "[*] 4 BULLS = WIN ";

cout << " 4 COWS = LOSE ";

cout << "\t\t\t\t HAVE FUN ";

cout << "=============================================================================== ";

cout << " " << endl << endl;

//MENU

cout << "MENU" << endl;

cout << "1. Set Puzzle" << endl;

cout << "2. Solve Puzzle" << endl << endl;

//do-while loop

do

{

// goto unconditional jump statement

in:

int attempts = 0;

//while loop for limited attempts

while (attempts <= 5 )

{

cout << " You have 5 attempts in this game to get 4 BULLS ";

cout << "THIS IS ATTEMPT " << attempts << "! ";

cout << "Enter a valid number choice from the given menu : ";

cin >> choice;

//Data Validation for characters

if (cin.fail())

{

cout << "Invalid choice. Please enter numbers only! ";

cin.clear();

cin.ignore(10000, ' ');

}

switch (choice)

{

case 1: setPuzzle();

doItAgain();

break;

case 2: solvePuzzle();

solved = 1;

break;

default: cout << " Pick a number from the given menu! ";

goto in;

}

if (solved)

{

if (bulls >= 4) //This is if bull happens to exceed more than 4 instead of a perfect 4

{

cout << " Congratulations!!, You Won! ";

cout << " BULLS = " << bulls << "\tCOWS = " << cows << " ";

break;

}

else if (cows == 4)

{

cout << " You lost! ";

cout << " BULLS = " << bulls << "\tCOWS = " << cows << " ";

break;

}

else

cout << " BULLS = " << bulls << "\tCOWS = " << cows << " ";

}

attempts++;

}

cows = 0;

bulls = 0;

} while (doItAgain());

return 0;

}

//Bool for doItAgain

bool doItAgain()

{

char choice;

bool again = false;

cout << " Do you want to start over to a new game? (Y/N): ";

cin >> choice;

while (!validateYesOrNo(choice))

{

cout << "Please enter a valid choice ";

cin >> choice;

}

if (choice == 'Y' || choice == 'y')

{

again = true;

cout << "Will do it again. Good Luck!" << endl;

}

else if (choice == 'N' || choice == 'n')

exit(0);

//exit(0)This will terminate the program.

return again;

}

//validation bool

bool validateYesOrNo(char again)

{

bool valid = true;

if (!(again == 'Y' || again == 'y' || again == 'N' || again == 'n'))

{

valid = false;

}

return valid;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!