nguess2.c (1019B)
1 /* Exercise 5.36 */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 int main() 7 { 8 int num, gnum; 9 int tests; 10 11 while(gnum = 1 + rand() % 1000) { 12 printf("\nI have a number between 1 and 1000.\n"); 13 printf("Can you guess my number?\n"); 14 printf("Please type your first guess.\n"); 15 scanf("%d", &num); 16 17 tests = 1; 18 while(num != gnum) { 19 ++tests; 20 if(num < gnum) 21 printf("Too low. Try again.\n"); 22 else 23 printf("Too high. Try again.\n"); 24 25 scanf("%d", &num); /* try again */ 26 } /* end while */ 27 28 if(tests == 10) 29 printf("Ahah! You know the secret!\n"); 30 else if(tests < 10) 31 printf("Either you know the secret or you go lucky!\n"); 32 else 33 printf("You should be able to do better!\n"); 34 35 printf("Would you like to play again (y or n)?\n"); 36 37 getchar(); /* take newline and, of course, ignore it */ 38 39 if(getchar() == 'n') /* not so strict :-) */ 40 break; 41 42 } /* end while */ 43 44 return 0; 45 } /* E0F main */ 46