taeyounkim LOG
Guessing a Number game codes 본문
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAXNUM 100
int secret_number; /*external value*/
int number_of_guess = 0; /*external value*/
void secret_num(){
srand((unsigned)time(NULL));
secret_number = rand() % MAXNUM + 1;
}
void process(){
int guess;
printf("Enter guess: ");
scanf("%d", &guess);
if(guess > secret_number){
printf("Too high. try again.\n");
number_of_guess++;
process();
}
else if(guess < secret_number){
printf("Too low. try again.\n");
number_of_guess++;
process();
}
else{
number_of_guess++;
printf("You won in %d guesses!\n\n", number_of_guess);
}
}
int main(void){
char response;
printf("Guess the secret number between 1 and 100: ");
do{
secret_num();
printf("\nA new number has been chosen\n");
number_of_guess = 0; /*this thread was needed in order to reset the number of guesses into zero every round. Otherwise, the number of guesses would be the aggregate of all the guesses from the previous rounds.*/
process();
printf("Play again? ");
scanf(" %c", &response);
} while(response=='y');
return 0;
}
'Career&Study > Coding Practice' 카테고리의 다른 글
pointer practice (decomposing fraction program) (0) | 2021.07.07 |
---|---|
Program to display "SEVEN SEGMENT DISPLAY" (0) | 2021.07.06 |
Game of Craps code, 그리고 stackoverflow.com (0) | 2021.07.02 |
function that returns the k(th) digit(from the right) in n(a positive integer) (0) | 2021.06.30 |
function that returns the day of year (0) | 2021.06.30 |