#include <iostream>
#include <string>
#include <random>
#include <vector>
#include <ctime>
#include <cstdlib>
//guessAmount is a global variable. In most cases this is bad practice, but because it is a
//constant and only an integer it is fine in this specific case.
const unsigned int guessAmount = 5;
bool NumberGuess(unsigned int& randNum)
{
unsigned int userInput = 0;
//Loop for the number of guesses that the user is allowed.
for (int i = 0; i < guessAmount; i++)
{
//Allows the user to guess the number
std::cout << "\n(Guess " << i + 1 << ") : ";
std::cin >> userInput;
if (userInput != randNum)
{
int guessesLeft = guessAmount - (i + 1);
if (userInput < randNum)
std::cout << "\nNo, my number is BIGGER than " << userInput << "! " << guessesLeft << " Guesses Left!";
else
std::cout << "\nNo, my number is LESS than " << userInput << "! " << guessesLeft << " Guesses Left!";
}
else
return true;
}
return false;
}
int main()
{
//Seeds srand with the current time.
std::srand(std::time(0));
//Creates a pseudo random number for the user to guess.
unsigned int randNum = rand() % 100 + 1;;
std::cout << "I have a number between 1 and 100. Can you guess the number?\n\nYou have " << guessAmount << " guessess.";
//Runs the function and outputs either a winning or a losing message.
if (NumberGuess(randNum))
std::cout << "\n\nYes! " << randNum << " is my number!";
else
std::cout << "\n\nSorry, you ran out of guesses. My number was " << randNum << ".";
}