Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7091821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:14:08+00:00 2026-05-28T08:14:08+00:00

I am trying to make a program where the computer has to guess a

  • 0

I am trying to make a program where the computer has to guess a number between 0 and 100 picked by the user(me). The program should guess the number within 7 tries which it does, but I’m having a problem with scanf(). When the program tries to guess the number, the user must tell it if the number guessed is too high, too low, or correct. The program works fine when the user just types a one character response, but freaks out when there is more than one character in “response”. So I would like to limit the response to just one character. How do I do this?

Thanks

#include <stdio.h>
#include <string.h>

int main() {
    char response[1];
    int numOfGuesses = 1;
    int min = 0;
    int max = 100;
    int guess = (max+min)/2;
    int end = 0;

    do {
        printf("%d) %d: (h)igh, (l)ow, or (c)orrect? ", numOfGuesses, guess);
        if (scanf("%1s", response) == 1) {

            printf("%s \n", response);
            if (strlen(response) > 1) {
                printf("D'oh! Wrong response!! \n");
            } else {
                if (response[0] == 'h') {
                    max = guess;
                    numOfGuesses++;
                } else if (response[0] == 'l') {
                    min = guess;
                    numOfGuesses++;
                } else if (response[0] == 'c') {
                    printf("Correct! It took %d turns. \n", numOfGuesses);
                    end = 1;
                } else {
                    printf("D'oh! Wrong response! \n");
                }

                guess = (max+min)/2;
            }

        } else {
            end = 1;
        }

    } while ( end == 0 );

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:14:08+00:00Added an answer on May 28, 2026 at 8:14 am

    Your response buffer is not null terminated, which pretty much makes it explode at

    if (strlen(response) > 1) {
    

    Make it response[2].

    Alternatively, you can use %c in scanf instead of %1s.

    Edit:

    Try calling this function after every scanf, it flushes the input buffer until the next newline, removing any invalid input after the first char.

    void flush_input()
    {
        int c = 0;
    
        do 
        {
            c = getchar();
        } while (c != EOF && c != '\n');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a program in Visual C# that has my one created
I'm trying to make a program which creates a spaceship and I'm using the
I am trying to make a simple guess my number game in c++ but
I am trying to make a program which does a very basic calculation, but
I am trying to make a simple calculator program where a user can opt
I'm trying to make a program where if it's installed on another computer, I
I'm trying to make my program check for bad user input. In this case,
I am trying to make a program that automatically corrects the perspective of a
I am trying to make a program to work on top of an existing
I'm trying to make a program that reads the value at a certain address.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.