Agreed CornedBee.
Here is what I have done today.
Code:
// THE GUESSING GAME by CC
// This is a simple game that will randomly
// pick a number that the user must try to
// guess. The user gets 5 tries. My second
// program actually.
// Last updated: 08/12/2003
//
// Changes:
// Added score system
// Code cleanups
// Added 'tries'
// Changes suggested by CBoard
// -Use new header files
// -Remove int Count
// -Changed RNG a bit
// Rearranged variables
// Moved system("cls") to it's own function
// Initialised variable y
// Added check for non-number guesses (thanks Prelude)
// Made numeric_check function
// Removed things out of main and into functions
#include <limits>
#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;
//Prototypes
void screen_clear();
void numeric_check(int y);
void intro();
void score_tries(int score, int tries);
void high_low(int x, int y);
int play_again(char again);
int enter_guess(int y);
int compare_x_y(int x, int y, int limit);
int right_wrong (int y, int score, int x);
// Global variables for RNG
const int LOW = 0;
const int HIGH = 100;
int main()
{
char again = 'y';
int x, y, final, limit;
int score = 0; // Starting value of user's score
int tries = 0; // How many times user has played
y = -1; // Giving y an arbitrary value
////////// RANDOM NUMBER GENERATOR ///////////
srand(static_cast<unsigned int>(time(0)));
//////////////////////////////////////////////
// The main loop
while (again == 'y')
{
limit = 0; // limit needs to be reset every loop repeat
screen_clear();
intro();
score_tries(score, tries);
// Part of the RNG
x = rand() % (HIGH - LOW + 1) + LOW;
y = compare_x_y(x, y, limit);
score = right_wrong(y, score, x);
again = play_again(again);
tries++;
}
return 0;
}
void screen_clear()
{
system ( "CLS" );
}
void numeric_check(int y)
{
cin.clear();
cin.ignore ( numeric_limits<streamsize>::max(), '\n');
cerr << "\nInvalid input. Enter a valid guess: ";
}
int right_wrong(int y, int score, int x)
{
if (y == x)
{
cout << "\nCongrads! You guessed the number.";
score++;
}
else
{
cout << "Sorry, you didn't get it.\n";
cout << "The number was: " << x;
score--;
// Keeping score always positive or equal
if (score < 0)
{
score = 0;
}
}
return score;
}
void intro()
{
cout << ":: THE GUESSING GAME ::\n";
cout << "I'm going to think of a number and you must try to guess it.\n";
cout << "You have five guesses. Don't worry, I'll give you a hint if you go wrong.\n\n";
cout << "The natural number is between 0 and 100. Ready? Let's go...";
}
int play_again(char again)
{
cout << "\n\n\nWould you like to play again? [y/n]\n";
cin >> again;
return again;
}
void score_tries(int score, int tries)
{
cout << "\n\n|| Your score is: " << score << '\n';
cout << "|| Tries: " << tries << "\n\n";
}
void high_low(int x, int y)
{
if (y > x)
{
cout << "The number: " << y << " is TOO HIGH!\n\n";
}
else if (y < x)
{
cout << "The number: " << y << " is TOO LOW!\n\n";
}
}
int enter_guess(int y)
{
while ( !(cin >> y) )
{
numeric_check(y);
}
return y;
}
int compare_x_y(int x, int y, int limit)
{
while ((y != x) && (limit < 5))
{
cout << limit + 1 << ". Enter your guess: ";
y = enter_guess(y);
high_low(x, y);
limit++;
}
return y;
}