0

I'm doing some practice questions to learn how to program in C... It's essentially a guessing game program. "Have the program initially guess 50, and have it ask the user whether the guess is high, low, or correct. If, say, the guess is low, have the next guess be halfway between 50 and 100, that is, 75. If that guess is high, let the next guess be halfway between 75 and 50, and so on." We're assuming that the user won't cheat. I'm rusty so I can't remember what operators I should use to increment the program's response based on the user's input. It'd be 2x the number, or half the number based on a response of H or L. Anyone know? I know it's easy, I just cant figure it out.

#include <stdio.h>
#include <ctype.h>

int main(int argc, const char * argv[])
{
int low;
int high;
int guess;
int response;
int toupper ( int );

do
{
    low = 1;
    high = 100;
    guess = 50;

    printf("Pick an integer from 1 to 100 and I will try to guess it.\n");
    printf("If I guess too low, respond with a L.\n");
    printf("If I guess too high, respond with a H.\n");
    printf("If I guess correctly, respond with a Y.\n");
    printf("Okay, is your number %d?\n",guess);

    while( (response=getchar()) != 'y')
    {
        if( response == 'l' )
        {
            // TODO: Ajust the value of low
            // TODO: Ajust the value of guess
            printf("Too low eh...well then, is it %d?\n",guess);
        }
        else if( response == 'h' )
        {
            // TODO: Ajust the value of high
            // TODO: Ajust the value of guess
            printf("Too high eh...well then, is it %d?\n",guess);
        }
        else if( response != '\n' )
        {
            fflush(stdin);
            printf("Sorry, I only understand L, H and Y. Please try again.\n");
        }
    }

    printf("Please enter R to restart, or Q to quit.\n");
    response=getchar();

    while ((response != 'q') && (response != 'r'))
    {
        if (response != '\n')
        {
            fflush(stdin);
            printf("Oops!  Please enter R or Q.\n");
        }
        response=toupper(getchar());
    } 
} while (response=='R'); 

return 0;
}

I'm struggling with what to put in where the hidden notes are.

Any insight would be appreciated!

2 Answers2

2
while( (response=getchar())) != 'y'
{

Please enclose != 'y' into the while() parenthesis.

timrau
  • 22,578
  • 4
  • 51
  • 64
1

Calling fflush(stdin) has undefined behaviour. Even though your platform might define fflush for an input stream, it's not standard and not portable. You should read all the input characters in the buffer and discard them. Apart from this, you have a syntax error in the while loop condition. Replace

while( (response=getchar())) != 'y'

with

while((response = getchar()) != 'y')

Also, replace fflush(stdin) with

int c; // define it at the beginning

// read and discard all characters up to and including
// the newline left in the stdin buffer
while((c = getchar) != '\n' && c != EOF) ; // the null statement

Please see this C FAQ - If fflush won't work, what can I use to flush input?

Also, see this question - I am not able to flush stdin

Community
  • 1
  • 1
ajay
  • 9,402
  • 8
  • 44
  • 71
  • @pixelpinch You can use the above `while` loop to read and discard unwanted characters in the stdin `buffer`. Define `int c;` at the top. Also, please read the two links I have mentioned. – ajay Apr 05 '14 at 17:20