#WSQ06 Pick a number. Playing on C++.

game

Hi people. I made a new program on C++ and it’s so cool. I like to do this stuff because is funny and also you learn new commands and variables that are useful on C++. On this game, you have to guess a number between 1-100. Check it and enjoy it!

 

Well, this program made me feel crazy because I didn’t know how to use a random function, but vualá, I checked a friend’s post and it helped me a lot! I searched for information on Sergio post and I found the formula and I saw the video and the pages that he used (https://sercho93.wordpress.com/2016/01/31/pick-a-number/).

And here is the screenshot of my code.

Haga click para ver el pase de diapositivas.

And the screenshot of the program running.

gpckan

On this program, we start using conditionals and cycles on C++. For the cycles, we use the commands DO-WHILE and that repeat an action until it’s false (When the While condition is false). So here, we want to find a random number between 1-100 and to make that, we use the do-while with the condition (number that the user choose != –different of– the number that the program choose), so when the function is true (different) it will repeat the program until you guess the program and the number is the same.

Also we have to use counters, to know how many guesses you need to get the correct number. To print the message of it is to low or to high, we use the conditional IF(that helps you to choose one or another way into the program, because if one it’s false, the another it’s true and with that you can take more than one way).

Another important thing is that the number has to be

maxresdefault

. 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 we 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).

This page may help you with random variables on C++:

Generando números aleatorios en C y C++

Check my complete code on GitHub:

https://github.com/eduardomrlsg/TC101/blob/master/PickANumber

maxresdefault

 

 

CC BY-SA 4.0 #WSQ06 Pick a number. Playing on C++. by eduardomoralesg is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.