Advertisement
Lisaveta777

Prata exs 8.5

Aug 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #define BEGIN 0
  3. #define END 100
  4.  
  5. //GOOD TO GIVE ANSWERS SUCH AS MM, LL, to figure EXACTLY how combination of scanf and getchar works
  6.  
  7. int main(void)
  8. {
  9.     char c;
  10.     int middle;
  11.     int begin,end = END;
  12.     begin = BEGIN;
  13.  
  14.     printf("pick a secret number between %d and %d\n", begin,end);
  15.  
  16.     do
  17.     {
  18.         middle = (end + begin)/2;
  19.         printf("i think you secret number is %d\n",middle);
  20.  
  21.         printf("input Y if i guessed right\n");
  22.         printf("L if secret number is less, M if it's more\n");
  23.  
  24.         scanf("%c",&c);
  25.         if(c=='L')
  26.         {
  27.             end = middle;
  28.  
  29.         }
  30.         else if(c=='M')
  31.         {
  32.             begin = middle;
  33.         }
  34.         else if(c=='Y')
  35.             break;
  36.         else
  37.             printf("your input is incorrect, try again\n");
  38.  
  39.     }while((c = getchar())!='Y'); //did it with this line, its totally ignored, when get to Y, which I totally
  40. //get. but why it's ignored all other times? have to rip it off everything, to figure it out
  41. //GOT it without ripping off! it's because scanf() gets symbol each time, and getchar() gets '\n'!
  42.      // }while(1);
  43.  
  44.  
  45.  
  46.  
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement