"Guess the number" game Any tips on how to improve my code and make it cleaner to read for other programmers? Formatting, spacing, etc.

#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;cstdlib&gt;
#include &lt;windows.h&gt;

using namespace std;

int main()
{
    int userInput;
    int number; 
    int min = 1;
    int max = 10;
    float playerTimesToPlay = 0; 
    int playAgain; 
    float tries = 3; 

    do
    {
        system("CLS");
        cout &lt;&lt; "Welcome to my number guessing game!" &lt;&lt; endl; 
        cout &lt;&lt; "====================================================" &lt;&lt; endl; 
        cout &lt;&lt; "The computer is generating a number between 1 and 10." &lt;&lt; endl; 
        cout &lt;&lt; "====================================================" &lt;&lt; endl; 
        srand(time(0)); 
        number = rand()% (max - min + 1)+min; 
        playerTimesToPlay = 0; 
        tries = 3; 



        do
        {

            cout &lt;&lt; "Please take a guess: ";
            if (!(cin &gt;&gt; userInput))
            {
            cout &lt;&lt; endl;
            cout &lt;&lt; "Please enter numbers only." &lt;&lt; endl;
            cin.clear();
            cin.ignore(10000,'\n');
            }


            while(userInput &gt; 10 || userInput &lt; 0)
            {
                cout &lt;&lt; "Pick a number BETWEEN 1 TO 10." &lt;&lt; endl; 
                cout &lt;&lt; endl; 
                userInput = NULL;
                playerTimesToPlay=0;
            }


            tries--;
            cout &lt;&lt; endl;
            cout &lt;&lt; "You picked: " &lt;&lt; userInput &lt;&lt; endl;
            cout &lt;&lt; "======================" &lt;&lt; endl;
            playerTimesToPlay++;

            if(userInput != number &amp;&amp; tries &gt; 0 &amp;&amp; userInput &lt;= 10)
            {
                cout &lt;&lt; "Sorry! Invalid choice. Please try again. You have " &lt;&lt; tries &lt;&lt; " guess(es) remaining." &lt;&lt; endl;
                cout &lt;&lt; endl;
            }

            else if(userInput != number &amp;&amp; tries == 0) 
            {
                cout &lt;&lt; "Sorry! Invalid choice." &lt;&lt; endl;
                cout &lt;&lt; "You have " &lt;&lt; tries &lt;&lt; " guesses remaining. You lose." &lt;&lt; endl;
                cout &lt;&lt; endl;
                cout &lt;&lt; "Play again? " &lt;&lt; endl;
                cout &lt;&lt; "1. Yes" &lt;&lt; endl;
                cout &lt;&lt; "2. No" &lt;&lt; endl;
                cout &lt;&lt; ": " ;
                cin &gt;&gt; playAgain;
            }

            if(userInput == number)
            {
                cout &lt;&lt; "Congratulations! You guessed the correct number!" &lt;&lt; endl; 
                cout &lt;&lt; "Thanks for playing!" &lt;&lt; endl;
                Sleep(2500);
            }




        }
        while (playerTimesToPlay &lt; 3 &amp;&amp; userInput != number);
    }
    while (playAgain == 1);


    system("Pause(0)");
    return 0;
}
Headers


You can remove stdlib.h as it is redundant to use both stdlib.h and cstdlib. The cstdlib is the c++ variant.
The same goes for the time.h header. Use ctime instead.
The Sleep() functions are virtually useless, I removed them and also the &lt;windows.h&gt; header along with it. Don't confuse your users with pauses such as those, not to mention the portability issues. Keep it simple and let the program run at its own pace on their machines.


Namespace


Don't make a habit of utilizing using namespace std;. You will run into trouble later on down the road if you choose to build something that another developer will make use of. It is heavily frowned upon as it will potentially cause namespace collisions, especially if included in a header file for say, a library.

Instead just explicitly call the functions with the scope operator:

std::cout &lt;&lt; "Don't muddy your scope!";



Variables


You have quite a few extra variables that can be removed, and also ones that could be more productive as a different data type.


The minimum and maximum variables should be declared as constants as they're not configurable anywhere in your code. Also, they could probably be a little more descriptive instead of just 'min' and 'max'. 

const int LOWER_NUMBER_BOUND = 1;
const int UPPER_NUMBER_BOUND = 10;

number is also pretty vague and should be renamed to infer its purpose.

int numberToGuess;

It appears you have two variables keeping track of the same data. You decrement tries when the player guesses incorrectly but at the same time you increment playerTimesToPlay. I would advise removing one of these and sticking to one variable that tracks the guess attempts remaining. It would also be a good idea to include a constant variable that holds the max attempts so you can reset the tracking variable after a game ends.

const int GUESS_ATTEMPTS = 3;
int guessAttemptsRemaining;

userInput can also be named more clearly. Something to solidify the function of the variable.

int userGuess;




Random


You're currently reseeding the random algorithm every time you play the game. This is unnecessary and it can be moved out of your loop.
Consider using the newer &lt;random&gt; instead, however the rand() function is simpler and more easily understood for a beginner. Also your distribution isn't critical so I wouldn't worry about this for the moment.


Logic


Inside of the inner do loop, the while statement can be altered to reflect when there are guesses remaining instead of using the incremented variable that was removed. It's also redundant to check if the user's guess is correct so that can also be removed.

do{...} while (guessAttemptsRemaining &gt; 0);

You can shrink the input validation and remove some of the output by simply asking them to enter a number between the bounds.

while (userGuess &lt; 1 || userGuess &gt; 10)
{
    std::cout &lt;&lt; "Please pick a number between 1 and 10: ";
    if (!(std::cin &gt;&gt; userGuess))
    {
        std::cin.clear();
        std::cin.ignore(10000, '\n');
    }
}

There is a logic error when checking the bounds of the user's input. Instead of checking if the number is less than 0 it should be if it is less than 1 as 0 is invalid input.

while (userGuess &lt; 1 || userGuess &gt; 10)

You check for the same conditions multiple times. Instead of repeating yourself you can restructure your logic to only check for a condition once. There is quite a bit of this going on so instead of pointing out every specific example I'll just go ahead and refactor it to show you what I mean.


Output


Instead of std::endl consider using \n instead. It is faster than a function call and also doesn't have the additional overhead of buffer manipulation that is included with std::endl.


Refactored Code

#include &lt;iostream&gt;
#include &lt;ctime&gt;
#include &lt;cstdlib&gt;

const int LOWER_NUMBER_BOUND = 1;
const int UPPER_NUMBER_BOUND = 10;
const int GUESS_ATTEMPTS = 3;

int main(int argc, _TCHAR* argv[])
{
    int userGuess;
    int numberToGuess;
    int guessAttemptsRemaining;
    int playAgain = 0;

    srand(time(0));

    do
    {
        system("CLS");
        std::cout &lt;&lt; "Welcome to my number guessing game!\n";
        std::cout &lt;&lt; "====================================================\n";
        std::cout &lt;&lt; "The computer is generating a number between 1 and 10.\n";
        std::cout &lt;&lt; "====================================================\n";

        numberToGuess = rand() % (UPPER_NUMBER_BOUND - LOWER_NUMBER_BOUND + 1) + LOWER_NUMBER_BOUND;
        guessAttemptsRemaining = GUESS_ATTEMPTS;

        do
        {

            userGuess = 0;

            while (userGuess &lt; 1 || userGuess &gt; 10)
            {
                std::cout &lt;&lt; "Please pick a number between 1 and 10: ";
                if (!(std::cin &gt;&gt; userGuess))
                {
                    std::cin.clear();
                    std::cin.ignore(10000, '\n');
                }
            }

            --guessAttemptsRemaining;
            std::cout &lt;&lt; "You picked: " &lt;&lt; userGuess &lt;&lt; ".\n";

            if (userGuess != numberToGuess)
            {
                std::cout &lt;&lt; "Sorry! You've guessed incorrectly!\n";

                if (guessAttemptsRemaining &gt; 0)
                {
                    std::cout &lt;&lt; "Please try again. You have " &lt;&lt; guessAttemptsRemaining &lt;&lt; " guess(es) remaining.\n";
                }
                else
                {
                    std::cout &lt;&lt; "You have no guess remaining. Play Again?\n";
                    std::cout &lt;&lt; "1. Yes\n";
                    std::cout &lt;&lt; "2. No\n";
                    std::cin &gt;&gt; playAgain;
                }

            }
            else
            {
                std::cout &lt;&lt; "Congratulations! You guessed the correct number!\n";
                std::cout &lt;&lt; "Thanks for playing!\n";
                break; //Add some way to continue here?
            }

        } while (guessAttemptsRemaining &gt; 0);

    } while (playAgain == 1);

    return 0;
}


Additional


I didn't cover everything so I may add more in the future. I also didn't go into extreme detail about the topics I went over so take my additions/alterations/etc. with a grain of salt and do your own research!