I recently got to a certain exercise in a book I am reading, and am looking for feedback. It is a program in which the user picks a number and the computer uses algorithms to guess it. If there is any way that I can improve my code, feedback is really appreciated!
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int compNumber = rand() % 100 + 1;
int yourNumber;
cout << "Please input a number between 1 and 100" << endl;
cin >> yourNumber;
cout << "Ok! I'm gonna try to guess it!" << endl;
while (compNumber != yourNumber)
{
if (compNumber > yourNumber)
{
do
{
--compNumber;
} while (compNumber != yourNumber);
}
else if (compNumber > yourNumber)
{
do
{
++compNumber;
} while (compNumber != yourNumber);
}
}
cout << "I guessed it! Your number is " << compNumber << endl;
return 0;
}
compNumber = yourNumber;
? \$\endgroup\$