1

I'm writing a small toy program that guesses the number chosen by the user, but when I run it, it gets stuck in:

Is your number higher or lower? (h/l): 

This is the code that I have written so far:

#include <stdio.h>
#include <stdlib.h> // includes srand() and rand()
#include <time.h>

#define MAX 100
#define MIN 1

int main(void)
{
  time_t t;
  char response;
  int lower = MIN, higher = MAX, guess;

  srand(time(&t)); //Initializes random number generator
  guess = (random() % 100) + 1; // Initial guess

  printf("Pick a number from 1 to 100. I'll try to guess it.\n");
  printf("Is your number %d? (y/n): ", guess);
  while ((response = getchar()) != 'y')
  {
    printf("Is your number higher or lower? (h/l): ");
    
    while ((response = getchar()) == 'h' || response == 'l')
    {
      if (response == 'h') {
        lower = guess;
        guess = (higher - guess) / 2;
        printf("Is it %d?: ", guess);
      } else if (response == 'l'){
        higher = guess;
        guess = (higher - guess) / 2;
        printf("Is it %d?: ", guess);
      }
    }
  }
  printf("Great!!\n");

  return 0;
}

For some reason it doesn't enter the second loop, where I check if response equals h or l.

Bobby Wan-Kenobi
  • 885
  • 9
  • 18
  • 5
    How do you handle the newline character which will follow each `'y'` or `'h'` or `'l'`, and be read by the next `getchar()`? – Weather Vane Sep 03 '20 at 18:40
  • 2
    I don't know why you were downvoted. The question is well enough presented (although the code formatting could be improved). – Weather Vane Sep 03 '20 at 19:05
  • I added this ``while (getchar() != '\n') continue;`` and it seems to work. Is that good practice? – Bobby Wan-Kenobi Sep 03 '20 at 19:08
  • 3
    IMO it would be better to input to a string with `fgets` and take a look at what it contains. If it's not useful, you just forget it and ask for another string. – Weather Vane Sep 03 '20 at 19:09
  • Hmm, haven't gotten to that part of the book yet )) Reading "C primer plus" by S. Prata – Bobby Wan-Kenobi Sep 03 '20 at 19:14
  • Rather than using `fgets`, just read and ignore whitespace. eg `while( ( response = getchar()) == 'h' || response == 'l' || isspace(response) ...` or similar. – William Pursell Sep 03 '20 at 19:19
  • @n.'pronouns'm. you should add some comments to https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list then – Antti Haapala -- Слава Україні Sep 03 '20 at 19:53
  • `char response` should be `int response`. Unintuitively, `getchar` does not actually return a `char` but an `int`. That is, it may return a character but also `EOF` which is an `int`. – Lundin Sep 04 '20 at 07:49
  • As for why the code doesn't work, it's the #1 newbie C FAQ of all time, trailing line feed characters in stdin. It has been asked and answered a million times before. – Lundin Sep 04 '20 at 07:52
  • You might receive some compensation for the downvote ;-) – Marged Sep 04 '20 at 07:54

1 Answers1

1

Ok, I found a way of dispose of newline characters and still being able to use getchar() to get input:

#include <stdio.h>
#include <stdlib.h> // includes srand() and rand()
#include <time.h>

#define MAX 100
#define MIN 1

char get1stChar(void);

int main(void)
{
  time_t t;
  char response;
  int lower = MIN, higher = MAX, guess;

  srand(time(&t)); //Initializes random number generator
  guess = (random() % 100) + 1; // Initial guess

  printf("Pick a number from 1 to 100. I'll try to guess it.\n");
  printf("Is your number %d? (y/n)\n"
     "Or is it Higher or Lower (h/l): ", guess);
  response = get1stChar();

  while (response != 'y')
  {
    printf("you're here\n");
    while (response == 'h' || response == 'l')
    {
      if (response == 'h') {
        lower = guess;
        guess = (higher - lower) / 2;
        printf("Higher: %d, Lower: %d\n", higher, lower);
        printf("Is it %d, higher or lower (y/h/l)?: ", guess);
      } else if (response == 'l'){
        higher = guess;
        guess = (higher - lower) / 2;
        printf("Higher: %d, Lower: %d\n", higher, lower);
        printf("Is it %d, higher or lower (y/h/l)?: ", guess);
      } 
      response = get1stChar();
    }
  }
  printf("Great!!\n");

  return 0;
}

char get1stChar(void)
{
  char ch;

  ch = getchar();
  while ((getchar()) != '\n');

  return ch;
}
Bobby Wan-Kenobi
  • 885
  • 9
  • 18