Number Guessing Game | While Loop | Repetition Structure
The following program randomly generates an integer greater than or equal to 0 and less than 100. The program then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message. Otherwise, the program checks whether the guessed number is less than the random number. If the guessed number is less than the random number generated by the program, the program outputs the message “Your guess is lower than the number. Guess again!”; otherwise, the program outputs the message “Your guess is higher than the number. Guess again!”. The program then prompts the user to enter another number. The user is prompted to guess the random number until the user enters the correct number.
To generate a random number, you can use the function rand from the header file cstdlib.
srand(time(0));
num = rand() % 100;
The first statement sets the seed, and the second statement generates a random number greater than or equal to 0 and less than 100. Note how the function time is used. It is used with an argument, that is, parameter, which is 0.
The program uses the bool variable isGuessed to control the loop. The bool variable isGuessed is initialized to false. It is set to true when the user guesses the correct number.
☛ Source code download link: https://drive.google.com/open?id=18HR...
--------------------------------------------------------------------------------------------------------
☛ C++ Tutorials Playlist: • C++ Programming Language
Earlier Tutorial Links are given bellow:
Tutorial#01: • Tutorial#01 - Introduction to programming ...
Tutorial#02: • Tutorial#02 - The Language of a Computer -...
Tutorial#03: • Tutorial#03 - Programming Methodologies - ...
Tutorial#04: • Tutorial#04 - History of C++ | C++ Program...
Tutorial#05: • Tutorial#05 - Basic Structure of C++ Progr...
Tutorial#06: • Tutorial#06 - Downloading and Installing D...
Tutorial#07: • Tutorial#07 - The Basics of C++ Program (P...
Tutorial#08: • Tutorial#08 - The Basics of C++ Program (P...
Tutorial#09: • Tutorial#09 - Data Types | C++ Programming...
Tutorial#10: • Tutorial#10 - The Basics of C++ Program (P...
Tutorial#11: • Tutorial#11 - Arithmetic Operators | C++ P...
Tutorial#12: • Tutorial#12 - Order of Precedence | C++ Pr...
Tutorial#13: • Tutorial#13 - Expressions in C++ | C++ Pr...
Tutorial#14: • Tutorial#14 - Increment and Decrement Oper...
Tutorial#15: • Tutorial#15 - Programming Example-1 ~ Conv...
Tutorial#16: • Tutorial#16 - Programming Example-2 ~ Make...
Tutorial#17: • Tutorial#17 - Control Structures ~ if Stat...
Tutorial#18: • Tutorial#18 - if...else Statement | C++ Pr...
Tutorial#19: • Tutorial#19 - Nested if else Statement |...
Tutorial#20: • Tutorial#20 - if-esle-if statement | C++ P...
Tutorial#21: • Tutorial#21 - Switch Statement | C++ Progr...
Tutorial#22: • Tutorial#22 - Programming Example-3 (Cable...
Tutorial#23: • Tutorial#23 - Programming Example-3 (Cable...
Tutorial#24: • Tutorial#24 - Repetitions | Control Struct...
Tutorial#25: • Tutorial#25 - Various forms of while loop ...
Tutorial#26: • Tutorial#26 - Programming Example-3 | Coun...
Tutorial#27: • Tutorial#27-Programming Example-4 | Find T...
☛ Follow us on Facebook: / thiscodinghub
☛ Join oue Whatsapp group: https://chat.whatsapp.com/LP50bFSGoK3...
☛ Website: https://thiscodinghub.blogspot.com
☛ Subscribe our channel for more videos.
/ @codinghub2680
Sur cette page du site, vous pouvez voir la vidéo en ligne Tutorial#28 - Programming Example-5 | Number Guessing Game | C++ Programming by Coding Hub durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Coding Hub 25 août 2019, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 138 fois et il a aimé 3 téléspectateurs. Bon visionnage!