I'm rehashing some of the basics of c++ and I just want to find useful points along the way of things I can do to improve my coding.
This program has the user enter a number, then the computer try to guess it, with some higher/lower logic.
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
srand(static_cast<unsigned int>(time(0))); //seed random number generator
int secretNumber = 0;
int tries = 0;
int guess;
int highRange = 100;
int lowRange = 0;
std::cout << "\tWelcome to Guess My Number\n\n";
std::cout << "\tPlease enter your number between 1 and 100:\n";
std::cin >> secretNumber;
do
{
do
guess = rand() % highRange + 1;
while (guess <= lowRange);
++tries;
if (guess > secretNumber)
{
std::cout << "Computer guess was too high! (" << guess << ")\n\n";
highRange = guess;
}
else if (guess < secretNumber)
{
std::cout << "Computer guess was too low! (" << guess << ")\n\n";
lowRange = guess;
}
else
{
std::cout << "\nI guessed it! (" << guess << ") in " << tries << " guesses!\n";
}
} while (guess != secretNumber);
return 0;
}