| Guess the Number | ||||||||
|
||||||||
|
This is a very simple program to make and run and really shouldn't give you any trouble. First we start off with the main program, depending on your IDE when you create a project you should get some starting code that looks similar to this:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << std::endl;
return 0;
}
Lets set the basics up first.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
int randomNumber = (rand()%10) + 1;
cout << "Welcome to the \"Guess the Number\" game.\n";
cout << "Please enter a number between 1 and 10.\n";
cout << "->";
return 0;
}
There are three changes I have made to the code that are of interest. The most mundane is that I have changed the output. I have added two more lines at the start that say "include<something>". This includes two extra libraries to the code - "cstdlib" and "ctime". These libraries allow us to use some functions that we need to use. The most interesting new lines are:srand(time(0)); int randomNumber = (rand()%10) + 1; "srand(int)" seeds the random number generator. As computers can only do what they are told it is impossible for programmers to create some algorithm that allows a random number to be generated. Therefore in programming we use pseudo-random numbers (false random numbers), these are generated from lists of numbers stored in the standard library, therefore if we asked for a random number in our program and ran it 5 times we would get the same number 5 times. In order to recieve different numbers we need to "seed" the random number generator - this means to give it a number that it can generate its pseudo-random numbers from. In order for this to be unique every time we run the program we seed it with the time (gained from the function "time(int)". This is a surprisingly big topic and if you would like to know more about it a search on google for pseudo random numbers will give results. The next line gets our random number and assigns it to a variable. The function "rand()" returns a random number between 0 and RAND_MAX (which is numer that varies between the versions of cstdlib - but will always be at least 32767). Therefore to get a number between 1 and 10 we need to get the remainder from the random number divided by 10, and add one. Now we need to add a loop and get the input, before the "return 0;" line enter this:
int guess;
cin >> guess;
while(guess != randomNumber){
cout << "Please enter another number.\n";
cout << "->";
cin >> guess;
}
cout << "Congratulations, you guessed correctly!\n";
We create a variable to store the guessed number and get the input from it then we enter into a loop, as long as "guess" doesn't equal "randomNumber" the loop will continue. All that happens in the loop is that the user is asked for some more input. We could add a bit more code to make it easier. At the start of the while loop (but still inside it) add this code:
if(guess < randomNumber)
cout << "Too low\n";
if(guess > randomNumber)
cout << "Too high\n";
And now you have a complete game. I have displayed the entire source code below incase you want it. Feel free to mess around with the code (which is by far the best way to learn to program). If you want a challenge change the program so that the user enters in the maximum number at the start and the random number is between that and 0.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand(time(0));
int randomNumber = (rand()%10) + 1;
cout << "Welcome to the \"Guess the Number\" game.\n";
cout << "Please enter a number between 1 and 10.\n";
cout << "->";
int guess;
cin >> guess;
while(guess != randomNumber){
if(guess < randomNumber)
cout << "Too low\n";
if(guess > randomNumber)
cout << "Too high\n";
cout << "Please enter another number.\n";
cout << "->";
cin >> guess;
}
cout << "Congratulations, you guessed correctly!\n";
return 0;
}
|
|||||||
|
If you would like to get in contact, please email me at: conn_peter@hotmail.com |