Hey there everyone
I've been reading posts on this forum for quite a while and really think that it rules.
Anyway I've been learning C++ on and off from sources on sites for the month. Today I purchased "C++ From the Ground Up" by Herbert Schildt and will work through it hopefully expanding my knowledge. I also have absolutely no previous background with regards to programming except for a little bit of Delphi (which I hated).
The main reason for this post is to get some comments on this little program thing I made. I'd just like to hear what people have to say and stuff, and if there are any tips or things regarding C++ that people would like to share. I'm really dedicated and want to learn this language well. I have years...
Here is the code:
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: 21/11/2003
//
// Changes:
// Added score system
// Code cleanups
// Added 'tries'
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
// Global variables for RNG
const int LOW = 0;
const int HIGH = 100;
int main()
{
char again ('y');
int x, y, limit, count, final;
////////// RANDOM NUMBER GENERATOR ///////////
// Declare variable to hold seconds on clock.
time_t seconds;
// Get value from system clock and place in seconds variable.
time(&seconds);
// Convert seconds to a unsigned integer.
srand((unsigned int) seconds);
//////////////////////////////////////////////
int score = 0; // Starting value of user's score
int tries = 0; // How many times user has played
// The main loop
while(again == 'y')
{
system ("cls");
cout << ":: THE GUESSING GAME ::"<< endl;
cout << "I'm going to think of a number and you must try to guess it." << endl;
cout << "You have five guesses. Don't worry, I'll give you a hint if you go wrong." << endl << endl;
cout << "The number is between 0 and 100. Ready? Let's go...";
cout << endl << endl << "|| Your score is: " << score << endl;
cout << "|| Tries: " << tries << endl << endl;
// Randomise the variable each time
// Part of the random number generator
x = rand() % (HIGH - LOW + 1) + LOW;
// Giving variables a value
count = 1; // Just used to number the guesses
limit = 0;
while(y!=x && limit<5) // loop when y not = x and limit less than 5
{
cout << count << ". Enter your guess: "; cin >> y;
if(y>x)
{
cout << "The number: " << y << " is TOO HIGH!" << endl << endl;
}
else if (y<x)
{
cout << "The number: " << y << " is TOO LOW!" << endl << endl;
}
count++;
limit++;
}
if(y==x)
{
cout << endl << "Congrads! You guessed the number.";
score++;
}
else
{
cout << "Sorry, you didn't get it." << endl;
cout << "The number was: " << x;
score--;
// Keeping score always positive or equal
if(score<0)
{
score=0;
}
}
cout << endl << endl << endl << "Would you like to play again? [y/n]" << endl;
cin >> again;
tries++;
}
return 0;
}