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 9131149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:05:49+00:00 2026-06-17T08:05:49+00:00

I’m going through Bjarne Stroustrup’s Programming Principles and Practice Using C++. I am on

  • 0

I’m going through Bjarne Stroustrup’s “Programming Principles and Practice Using C++”. I am on chapter 4, exercise 4.

The excercise is as follows:

Write a program to play a numbers guessing game. The user thinks of a number between 1 and 100 and your program asks questions to figure out what the number is (e.g. “Is the number you are thinking of less than 50?”). Your program should be able to identify the number after asking no more than seven questions. Hint: Use the < and <= operators and if-else constructs.

Now that is great, and I’ve managed to implement that fine.

I’d thought I’d try and push myself and try and implement this using a loop and adjusting lower or upper bounds each time.

Here is my code:

#include "std_lib_facilities.h"

int main( ){
  int count = 0;
  int lowerBound = 0;
  int upperBound = 100;
  string userInput = "";

  while ( lowerBound != upperBound ){
    // Increment count
    ++count;

    int halfRange = 0;

    // Make halfRange a while number, round up if any decimal portion.
    double range = ( upperBound - lowerBound ) / 2;

    int rangeDelta = range - (int)range;

    if ( rangeDelta != 0 )
      halfRange = (int)range + 1;
    else
      halfRange = range;

    cout << count <<": Is your number between " << lowerBound << " and " << lowerBound + halfRange << "? ";
    cin >> userInput;

    // Reset the bounds
    if ( userInput == "y" || userInput == "Y" )
      upperBound -= halfRange;
    else if ( userInput == "n" || userInput == "n" )
      lowerBound += halfRange;
    else {
      --count;
      cout << "Error! Answer could not be understood.";
    }
  }

  cout << "lowerBound: " << lowerBound << ", upperBound: " << upperBound << "\n\n";
  cout << "Your number is: " << lowerBound << "\n";

  return 0;
}

The problem? Well, it occurs when it gets to numbers where there is a decimal portion and using integer division, which throws the decimal part away. If you use the number 48, the program guesses to 47 and 47.

Any clues to get me going? I think I’m quite close, but will appreciate some help.

Thanks,

Matt

  • 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-17T08:05:50+00:00Added an answer on June 17, 2026 at 8:05 am

    I think that there is still an issue in your code (even if the bugs signaled before are fixed): it should be clear that upperbound and lowerbound are both included in possible numbers remaining and this is not clear from your code: at the first question, if I answer ‘y’, the new interval is [0,50] and if I answer ‘n’, the new interval is [50,100], which is wrong! 50 should not be included in the second interval.

    To fix this, you should change the update of your bounds to something like:

    if ( userInput == "y" || userInput == "Y" )
       upperBound = lowerBound + halfRange;
    else if ( userInput == "n" || userInput == "n" )
       lowerBound = lowerBound + halfRange + 1;
    

    Now there is still a problem with the last question, which remains the same forever. The problem is that when range=1, you have halfrange=1 as well and the question keeps the same.

    To solve that issue, you should round down range. Just define it like:

    int halfRange = ( upperBound - lowerBound ) / 2;
    

    and now your code should work. Here is the code I would use for the while loop:

    while ( lowerBound != upperBound ){
    // Increment count
    ++count;
    
    int halfRange = ( upperBound - lowerBound ) / 2;
    int midpoint = lowerBound + halfRange;
    
    cout << count <<": Is your number between " << lowerBound << " and " << midpoint << "? (both included) ";
    cin >> userInput;
    
    // Reset the bounds
    if ( userInput == "y" || userInput == "Y" )
      upperBound = midpoint;
    else if ( userInput == "n" || userInput == "n" )
      lowerBound = midpoint + 1;
    else {
      --count;
      cout << "Error! Answer could not be understood.";
    }
    

    }

    Remark that now we only use integers, no double at all! If you use integers, try to avoid passing by double, and use rather integer division and modulo to work with them (and do not use double to represent integers).

    Hope it helps!

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.