Any tips on how to improve my code and make it cleaner to read for other programmers? Formatting, spacing, etc.
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <cstdlib>
#include <windows.h>
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 << "Welcome to my number guessing game!" << endl;
cout << "====================================================" << endl;
cout << "The computer is generating a number between 1 and 10." << endl;
cout << "====================================================" << endl;
srand(time(0));
number = rand()% (max - min + 1)+min;
playerTimesToPlay = 0;
tries = 3;
do
{
cout << "Please take a guess: ";
if (!(cin >> userInput))
{
cout << endl;
cout << "Please enter numbers only." << endl;
cin.clear();
cin.ignore(10000,'\n');
}
while(userInput > 10 || userInput < 0)
{
cout << "Pick a number BETWEEN 1 TO 10." << endl;
cout << endl;
userInput = NULL;
playerTimesToPlay=0;
}
tries--;
cout << endl;
cout << "You picked: " << userInput << endl;
cout << "======================" << endl;
playerTimesToPlay++;
if(userInput != number && tries > 0 && userInput <= 10)
{
cout << "Sorry! Invalid choice. Please try again. You have " << tries << " guess(es) remaining." << endl;
cout << endl;
}
else if(userInput != number && tries == 0)
{
cout << "Sorry! Invalid choice." << endl;
cout << "You have " << tries << " guesses remaining. You lose." << endl;
cout << endl;
cout << "Play again? " << endl;
cout << "1. Yes" << endl;
cout << "2. No" << endl;
cout << ": " ;
cin >> playAgain;
}
if(userInput == number)
{
cout << "Congratulations! You guessed the correct number!" << endl;
cout << "Thanks for playing!" << endl;
Sleep(2500);
}
}
while (playerTimesToPlay < 3 && userInput != number);
}
while (playAgain == 1);
system("Pause(0)");
return 0;
}