C language because that guessing numbers games

After the initial research of C language, in bespeak to boost the programming ability, you can knock some code the takes a tiny trouble, guess: v the number is a great choice. To establish the game, we should first know the basic idea the production.

You are watching: Random number guessing game c++

The computer system should generate a number at random (1-100 is selected to rise the difficulty and attention of guessing)The player entry the guessed number and the computer system gives feedback.Players discover the feedback and also proceed to further operations. (for example: the number guessed is also large, the number guessed is also small, the number guessed is exactly (whether to play a game again))

After discovering the basic idea, we have the right to write the code by hand

First that all, pat the video game must have access to the interface, permit others know what the game is.

#Include / / never ever forget the header filevoid menu()//No return value is required printf("*********Guess the number******** ");printf("********1.PLAY******** ");printf("********0.EXIT******** "); then you have the right to write the end the key function

int main() srand((unsigned int)time(NULL));//Generating random numbersint input = 0;do//do while is chosen due to the fact that it deserve to be executed very first and judged later to meet existing requirementsmenu();printf("Please select(1/0) ");scanf("%d", &input);switch (input)//Here, if you usage the if statement, it"s a little cumbersomecase 1:game();//The key body the the game, I will write belowbreak;case 0:printf("Quit the game ");break;default:printf("Wrong input, please input again ");//Dealing with wrong inputbreak; while(input);//0 is false and also non-0 is true, so input is supplied to judge whether the loop is executedreturn 0; then there"s the key play - the key body of the video game According come the idea, the first thing is to generate random numbers edge Generates a pseudorandom number. Int rand( void ); The rand function returns a pseudorandom creature in the variety 0 to RAND_MAX. Use the srand duty to particle the pseudorandom-number generator before calling rand. The explanation of rand in MSDN is as above. We can know the the return worth of edge is from 0 to RAND_MAX (0x7fff, decimal 32767), then whether it deserve to play the role of generating arbitrarily numbers? after ~ testing, it can create random numbers, yet it is not an overwhelming to find that the numbers space the same twice as soon as compiling again, which provides the game less interesting.

srand sets a random beginning point. Void srand( unsigned int seed );

So we need srand to administer a beginning point because that its generation, however srand also needs a variable random number seed to assist rand generate random numbers. For this reason we present the principle of time stamp (time stamp: the difference between the existing time and also the starting time of the computer, in seconds; the beginning time that the computer: 8:00:00 a.m. On January 1, 1970). The time() function returns the moment stamp, The time need to be an altering all the time, and so is the timestamp. Due to the fact that only one random seed is needed each time, the explain is inserted in the main function.

See more: Ann U Visa Fiscal Year 2017, How Long Will It Take To Get A U Visa

void game(){ int ret = rand() % 100+1;//Because the variety of rand worth is 0 ~ 32767, so% 100 and also + 1 make it ret)printf("Guess big ");else if (num Finally, compose the main program again

#include#Include / / to use the time() functionvoid menu()printf("********Guess the number******** ");printf("********1.PLAY******** ");printf("********0.EXIT******** ");void game(){int ret = rand() % 100+1;//Because the range of rand value is 0 ~ 32767, so% 100 and + 1 make it ret)printf("Guess big ");else if (num ps: because that the first time I created a an innovation blog, there space still plenty of things to it is in corrected. I hope you can allude out mine shortcomings.

posted by plgmelodyboy at jan 21, 2021 - 4:49 pm Tag: C Programming Game advance