#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int main();
int getRandom();
int game();
int playAgain() //ask if user wants to play agian
{
char answer, y, n; //answer is the response to the question
//y and n are the answers the user can give
cout << "Do you want to play again (y or n)?: ";
cin >> answer;
if(answer == y) //if the user types y then it starts the main()
main();
else if(answer == n) //If n then exits
cout << "Thanks for playing" << endl;
return 0;
}
int getRandom() // genurates a random #
{
int max, value; // max is the max value the user picks
// value is the random number
cout << "Enter the maximum random number value: ";
cin >> max;
srand( time(0) );
value = rand() % max + 1; //random number equation
return value;
}
int game() // ask the user their guess
{
int guess, random, count;
random = getRandom();
do
{
cout << "What is your guess?: ";
cin >> guess;
if(guess < random) // The if statements checks to see if they are to high or low
cout << "Too low." << endl; // then gives an answer
if(guess > random)
cout << "To High." << endl;
}while( guess != random ); // ask the question until guess = random
count += 1; // keeps track of trys
cout << "You got it in " << count << " try(s)" << endl;
return 0;
}
main() // runs the game
{
do
{
game();
}while(playAgain());
return 0;
}