C language to guess the size of the game

  • 2020-06-23 01:35:30
  • OfStack

This article Shared C language for you to guess the size of the specific code, for your reference, the specific content is as follows

void menu()


void menu()
{
 printf("********    Guessing game    ********\n");
 printf("***********  1. start   *************\n");
 printf("***********  0. exit   *************\n");
}

Simple 1 menu for display.

void game()


void game()
{
 int a=0;
 int b=0;
 b=rand()%100; 
 while(1)
 {
 printf(" Please enter the number you want to guess \n");
 scanf("%d",&a);
 if(a>b)
 {
  printf(" Guess the \n");
 }
 else if(a<b)
 {
  printf(" Guess a little \n");
 }
 else
 {
  printf(" Ever. \n");
  break;
 }
 }
}

The game function defines two variables, one for the number we guessed b and one for the number we entered a. We used an rand function to generate a random number. We set the number between 0 and 100. Of course, we needed the relevant header file to use this function.


#include <stdlib.h>
#include <time.h> 

int main()


int main()
{
 int input=0;
 do
 {
 menu();
 printf(" Please select a ");
 scanf("%d",&input);
 switch(input)
 {
 case 1:
  printf(" Start playing games \n");
  game();
  break;
 case 0:
  printf(" Quit the game \n");
  break;
 default:
  printf(" The wrong choice \n");
  break;
 }
 }while(input);
 return 0;
}

Loop our menu with do whie. switch statement is used to implement the selection, select 1 to start the game, select 0 to exit the game, and the corresponding Yong printf prompts.

code

Here's all of our code:


#include<stdio.h>
#include <stdlib.h>
#include <time.h> 
void menu()
{
 printf("********    Guessing game    ********\n");
 printf("***********  1. start   *************\n");
 printf("***********  0. exit   *************\n");
}
void game()
{
 int a=0;
 int b=0;
 b=rand()%100; 
 while(1)
 {
 printf(" Please enter the number you want to guess \n");
 scanf("%d",&a);
 if(a>b)
 {
  printf(" Guess the \n");
 }
 else if(a<b)
 {
  printf(" Guess a little \n");
 }
 else
 {
  printf(" Ever. \n");
  break;
 }
 }
}
int main()
{
 int input=0;
 do
 {
 menu();
 printf(" Please select a ");
 scanf("%d",&input);
 switch(input)
 {
 case 1:
  printf(" Start playing games \n");
  game();
  break;
 case 0:
  printf(" Quit the game \n");
  break;
 default:
  printf(" The wrong choice \n");
  break;
 }
 }while(input);
 return 0;
}

Related articles: