Pick a Number

Hey! Do you want to play? Yes? You’re lucky because here’s a gaming code!

Intructions:

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.

The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

Yeah! A guess game! And this is the code:

pick

pick1

We’re using the same things as the past codes but now there’s something different. We need to use CYCLES with a DO-WHILE command.

What is DO-WHILE? The do-while loop use the test condition at the end of the loop.  Having the test condition at the end, guarantees that the body of the loop always executes at least one time (http://mathbits.com/MathBits/CompSci/looping/dowhile.htm).


This command will repeat the cycle until the condition that we gave becomes true. Then the loop is over. In our code we combine IF and DO to create the game. 

Another important thing is that the number has to be random. So, we use the function SRAND() and declare the variable number as the random number. To make it functional we have to introduce the TIME function too in the SRAND funtion.

SRAND() ->  (Library: <stdlib.h>)

TIME ->  (Library: <time.h>

Combining this

have:

SRAND (TIME(NULL));

NUM = RAND()%101;

With “SRAND (TIME(NULL));” we obtain completely random numbers and with “NUM = RAND()%101;” we declare de variable (write % and a limit for the random numbers plus 1).

And that’s it. Now we can play it!

See you later!

Code Link:

https://www.dropbox.com/s/i5pqun0lilehc3t/PickANumber.cpp?dl=0

This blog helps me a lot with this topic: https://darkchicles.wordpress.com/2010/05/12/ generando-nmeros-aleatorios-en-c-y-c/

CC BY-SA 4.0 Pick a Number by sercho93 is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.