SHARE
TWEET

Untitled

a guest Jun 12th, 2019 76 in 3 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. PROJECT: C Premier Plus exercise 8 - 5
  3. PURPOSE: "Modify the guessing program of Listing 8.4 so that it uses a more
  4.           intelligent guessing strategy. For example, Have the program
  5.           initially guess 50, and have it ask the use whether the guess is
  6.           high, low or correct. If, say, the guess is low, have the next guess
  7.           be half way between 50 and 100, that is, 75. If the guess is high,
  8.           let the next guess be half way between 75 and 50, and so on. Using
  9.           this Binary search strategy, the program quickly zeros in on the
  10.           correct answer, at least if the user does not cheat.
  11.   DATES: 30 may '19, afternoon -
  12. */
  13. #include <stdio.h>
  14. /*
  15.     The program will ask the user if the want to play a game, if they accede
  16. offer the first guess and ask if it is correct
  17.     IF The guess should go higher add a variable equal to a half of the guess
  18. and keep adding until the users says lower
  19.     If the question should go lower Split the guess by a variable that starts
  20. at a half of the guess and keep halving until the user says higher
  21. */
  22. int main (void)
  23. {
  24.     int Guess = 50, UpStep = 25, LoStep = 2;
  25.     char Status, Answer = '\0', Step = '\0'; //I already tried not initializing
  26.                                              //these variables but the same
  27.                                              //error keeps happening
  28.    
  29.     printf("Lets play a game, shall we?\n[Y]es?\n[N]o?\n");
  30.     scanf("%c", &Status);
  31.    
  32.     if ((Status == 'N') || (Status == 'n'))    /*exit message*/
  33.     {
  34.         printf("\nOh well, see you then");
  35.         return 0;
  36.     }
  37.    
  38.     if ((Status == 'Y') || (Status == 'y')) /*begin of the game*/
  39.     {
  40.         printf("\nThink of a number from 1 to 100");
  41.         printf("\nIs it %d ?", Guess);
  42.         printf("\n[Y]es or [N]o?\n");
  43.         scanf("%c", & Answer);//The program is going from this line to the
  44.                               //error message on line 81
  45.        
  46.         if ((Answer == 'Y') || (Answer == 'y')) /*congrats message*/
  47.         {
  48.             printf("\nGreat!");
  49.             printf("\nWanna play again?");
  50.             scanf("%d", &Status);
  51.         }
  52.        
  53.         if ((Answer == 'N') || (Answer == 'n')) /*game loop*/
  54.         {
  55.             printf("\nShould it be [h]igher or [l]ower?");
  56.             scanf("%c", & Step);
  57.            
  58.             if ((Step == 'H') || (Step == 'h')) /*Up Movement*/
  59.             {
  60.                 Guess = Guess + UpStep;
  61.                 UpStep = UpStep + 1;
  62.                 LoStep = LoStep - 1;
  63.             }
  64.            
  65.             if ((Step == 'L') || (Step == 'l')) /*Down movement*/
  66.             {
  67.                 Guess = Guess / LoStep;
  68.                 LoStep = LoStep + 1;
  69.                 UpStep + UpStep / LoStep;
  70.             }
  71.            
  72.             else /*error message*/
  73.             {
  74.                 printf("\nThat's not an acceptable answer");
  75.                 printf("\nPlease use H or L");
  76.                 scanf("%c", & Step);
  77.             }
  78.            
  79.             printf ("Is it %d ?");
  80.             scanf("%c", & Answer);
  81.         }
  82.        
  83.         else /*error message*/
  84.         {
  85.             printf("\nThat's not an acceptable answer");
  86.             printf("\nPlease use Y or N");
  87.             scanf("%c", & Answer);
  88.         }
  89.     }
  90.    
  91.     else /*error message*/
  92.     {
  93.         printf("\nThat's not an acceptable answer");
  94.         printf("\nplease use 'y' or 'n'");
  95.         scanf("%d", &Status);
  96.     }
  97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top