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

  • SEARCH
  • Home
  • 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 8534437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:11:49+00:00 2026-06-11T10:11:49+00:00

Possible Duplicate: Program not waiting for cin I wrote the following code: #include <iostream>

  • 0

Possible Duplicate:
Program not waiting for cin

I wrote the following code:

#include <iostream>
using namespace std;

void search(int pre, int a, int b, int x) {
    char c;
    cout << "Is the number " << ((x == 2) ? b : a) << endl;
    c = cin.get(); ////// this does not block
    if (c == 'y') return;

    else {
        cout << "Is the number " << ((x == 2) ? b : a) << " closer to your number than " << pre;
        c = cin.get();

        if (c == 'y') {
            search(a, a, (a + b) / 2, 2);
        } //c=='y'
        else search(a, (a + b) / 2, b, 1);
    }
}

int main() {
    int N;
    cout << "Enter N? ";
    cin >> N;

    search(N, 1, N, 1);
    return 0;
}

No need to worry if you don’t understand the logic because my question is not regarding that.

In the search function, there are two cin.get(), where i need the user to enter a character. My problem is that the the program blocks for input only after the second cin.get().

For example:

 Is the number 7  //program doesn't wait after this
 Is the number 7 closer to your number than 8  //program blocks here for an input

Why does it do so?

  • 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-06-11T10:11:50+00:00Added an answer on June 11, 2026 at 10:11 am

    There are at least two problems in your code. The first is that you’re
    leaving characters in the buffer after inputting N. The simplest
    solution is to just add a call to std::cin.ignore( INT_MAX, '\n' );
    after std::cin >> N;; a better solution (because it allows for more
    error checking) would be to use std::getline to read the complete
    line, then parse it using std::istringstream.

    The second problem is that you’re assigning the results of
    std::cin.get() into a char. std::cin.get() returns an int,
    which may be EOF. And you really want to check whether it is EOF
    before converting the int to char: you cannot check after because
    either some legal char will compare equal to EOF (plain char is
    signed), or the char will never compare equal to EOF (plain char
    is unsigned). The other alternative is to do something like:

    if ( !std::cin.get( c ) ) {
        //  EOF or error seen...
    }
    

    each time you want to read a char. (This might be better in your
    case, since if you do read EOF, all further calls to std::cin.get()
    will return EOF.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Size of character ('a') in C/C++ The following program #include <stdio.h> int
Possible Duplicate: Why does sizeof(x++) not increment x? #include<stdio.h> int main(void) { double num=5.2;
Possible Duplicate: remove_if equivalent for std::map Yesterday i wrote a program, which use multiset
Possible Duplicate: IOS: one account two iOS developer program excuse me if it's not
Possible Duplicate: Objective-C style formatter I have wrote a program and wonder if there
Possible Duplicate: Is Java pass-by-reference? Java pass by reference For the following Java program,
Possible Duplicate: Why an executable program for a specific CPU does not work on
Possible Duplicate: A simple program to CRUD node and node values of xml file
Possible Duplicate: C# - How to get Program Files (x86) on Windows Vista 64
Possible Duplicate: Can I run from command line program created by Eclipse? I am

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.