#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main()
{
int num1, num2;
int userGuess;
srand(time(0));
int userTries = 0;
int computerNum{0};
cout<<"Please enter 2 integers: ";
cin>>num1>>num2;
computerNum = rand() % (num2 - num1) + num1; // <--
cout << computerNum << '\n'; // <-- just for testing purposes
cout<<"I'm thinking of a number between " << num1 <<" and " <<num2<<".\n";
do
{
userTries++;
cout<<"Enter guess: ";
cin>>userGuess;
cout<<" - ";
if(userGuess < computerNum)
{
cout<<"Too Low\n";
}
elseif(userGuess>computerNum)
{
cout<<"Too High\n";
}
elseif(userGuess == computerNum)
{
cout<<"Correct, it took you " <<userTries<<" tries to guess my number.";
}
}
while(userGuess != computerNum);
return 0;
}
Please enter 2 integers: 15 20
15
I'm thinking of a number between 15 and 20.
Enter guess: 19
- Too High
Enter guess: 12
- Too Low
Enter guess: 15
- Correct, it took you 3 tries to guess my number.Program ended with exit code: 0