0

I am trying a number guessing game that I found, it seems that no matter what I choose, it keeps saying the number I've chosen is less than, or more than. I want to implement it using binary search, but have no idea how to do this. How can I make it happen?

Codes:

#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;

int main() {
      srand(time(0));
      int number;
      number = rand() % 100 + 1;
      int guess;
      do {
            cout << "Enter a number of your choice b/w 1-100: ";
            cin >> guess;
            if (guess < number)
                  cout << "Sorry, try again, it's smaller than the secret number!" << endl;
            else if (guess > number)
                  cout << "Sorry, try again, it's bigger than the secret number!" << endl;
            else
                  cout << "The number is correct! Congratulations!" << endl;
      } while (guess != number);
      system("PAUSE");
      return 0;
}
  • 1
    So you want to code to auto guess the number or you want he user to guess the number? – NathanOliver Apr 25 '17 at 14:43
  • I want the user to guess it, for example the secret number is 58, the user keep trying to type in some number, if he get it, he does, if not, program keep asking. I used rand() to make the game more interesting as it generate a random number always. – Uxellodunon Apr 25 '17 at 14:44
  • 3
    What exactly do you want to implement binary search for if the user is the one doing the searching? – François Andrieux Apr 25 '17 at 14:46
  • I'm still learning, I found this, but I had no idea if it's a binary search. What can I do to make it a binary search for this game? – Uxellodunon Apr 25 '17 at 14:49
  • one note: rand() is obsolete (have a look at ) and system() just for a pause is not a good reason to do so – The Techel Apr 25 '17 at 14:50
  • 1
    @Uxellodunon Yes. This is an example of how a person would do a binary search to find a value. It not exactly a true binary search since the user doesn't have to guess the new mid point but they should guess a new number that is closer to the number to guess every time as they know which direction the number is. – NathanOliver Apr 25 '17 at 14:52
  • Yes Nathan, this is what I'm trying to do... But the code isn't working, no matter what I write, it won't guess the number. What are the issues? :/ – Uxellodunon Apr 25 '17 at 14:55
  • Works for me. See this example image of my run of the code: https://i.stack.imgur.com/SEkeb.png – NathanOliver Apr 25 '17 at 14:56
  • @Uxellodunon I'm very confused by your messages, they seem contradictory. Are you trying to use binary search instead of `rand` to come up with the number that the user has to find? A binary search is an algorithm that you can use to find a specific element in a sorted range of values. I don't see any way it could be useful in your example, unless you changed it so the program tried to "guess" a value chosen by the user. – François Andrieux Apr 25 '17 at 15:03
  • Hey @NathanOliver how did you open it in Visual? I used visual as well, I clicked C++ / General / Empty project, then I add new items from source = c++ file. Didn't work. Francois, like I said man I have no idea if things are correct or not. – Uxellodunon Apr 25 '17 at 15:43
  • 1
    @Uxellodunon Try making a simple hello world program. Once you get that working just copy and past the code from this question over the code in the hello world program and recompile. – NathanOliver Apr 25 '17 at 15:47
  • @NathanOliver I did, still didn't work. When I type in 100, it says your number is bigger than 100... When I write above 100, the program keep sending the message "Sorry, try again, it's bigger than the secret number!". – Uxellodunon Apr 25 '17 at 15:53
  • Your reading your own prompts wrong. Your prompt is telling you that 100 is bigger than the number to guess. – NathanOliver Apr 25 '17 at 15:54
  • In *Sorry, try again, it's bigger than the secret number* **it's** refers to the number you just entered. – NathanOliver Apr 25 '17 at 15:55
  • @NathanOliver Ya, I have no idea what is wrong? This is what I'm getting: – Uxellodunon Apr 25 '17 at 15:59
  • Enter a number of your choice b/w 1-100: 50 Sorry, try again, it's bigger than the secret number! Enter a number of your choice b/w 1-100: 1 Sorry, try again, it's smaller than the secret number! Enter a number of your choice b/w 1-100: 70 Sorry, try again, it's bigger than the secret number! Enter a number of your choice b/w 1-100: 60 Sorry, try again, it's bigger than the secret number! Enter a number of your choice b/w 1-100: 101 Sorry, try again, it's bigger than the secret number! Enter a number of your choice b/w 1-100: – Uxellodunon Apr 25 '17 at 15:59
  • I've chosen one and it says smaller. – Uxellodunon Apr 25 '17 at 16:00
  • I have not enought reputation to comment, so... I am not completly sure, but when you use cin in order to get information from user keyboard, the input is not an integer but a string. I think you must cast your input or even use C++ method. You can use the information provided in the following link: http://stackoverflow.com/questions/7663709/convert-string-to-int-c – César HM Apr 25 '17 at 15:05

2 Answers2

1

this should do this thing

#include <cstdlib>
#include <ctime>
#include <iostream>

using namespace std;
int guessNum(int lb,int ub,int number){
    int lowerBound=lb,upperBound=ub;
    int guess = (lowerBound+upperBound)/2;
    if (number>guess){
        lowerBound = guess;
        guessNum(lowerBound,upperBound,number);
    }
    else if(number < guess){
        upperBound=guess;
        guessNum(lowerBound,upperBound,number);
    }

    else
        return guess;
}

int main() {
    srand(time(NULL));
    int number;
    number = rand() % 100 + 1;
    int guess;

          std::cout<<number << " = " <<guessNum(1,100,number);


    return 0;
}
Prashant Bhardwaj
  • 86
  • 1
  • 2
  • 11
  • My understanding is that the User is trying to guess the Computer's number. With your example, the Computer is guessing a random number. – Thomas Matthews Apr 25 '17 at 18:01
  • @ThomasMatthews before downvoting answer, Read this line in OP " I want to implement it using binary search, but have no idea how to do this. How can I make it happen?" most probably implementing binary search means that computer find the random number by implementing binary search , but again I can be WRONG – Prashant Bhardwaj Apr 25 '17 at 18:34
  • Read this line in the comments from the OP: *"I want the user to guess it, for example the secret number is 58, the user keep trying to type in some number, if he get it, he does, if not, program keep asking."* – Thomas Matthews Apr 25 '17 at 19:22
0

Understand the game

The software choose randomly a number between 0-100, and you need to find it ¿right? , the software gave you some clues but you never found the number. so, what will be the solution?

Cheat the game

When the things goes wrong I prefer to be clear. So, ask to the software the number and you will know which number is. I do this every time that I need to know what is happen behind scenes

#include <cstdlib>
#include <time.h>
#include <iostream>

using namespace std;

int main() {
      srand(time(0));
      int number;
      number = rand() % 100 + 1;
      int guess;
      do {
            cout << "Enter a number of your choice b/w 1-100: ";
            cin >> guess;
            // with this line ↓ you could see what is happen
            cout << "Your number is " << guess << " and the secret number is " << number << endl;
            if (guess < number)
                  cout << "Sorry, try again, it's smaller than the secret number!" << endl;
            else if (guess > number)
                  cout << "Sorry, try again, it's bigger than the secret number!" << endl;
            else
                  cout << "The number is correct! Congratulations!" << endl;
      } while (guess != number);
      system("PAUSE");
      return 0;
}

Finnally I think it is a concept mistake understanding the game, because when it says "Sorry, try again, it's bigger than the secret number!" it is refer that the number that you input by keyboard is bigger that the secret number. I really hope this line make clear the things for you. Regards

Community
  • 1
  • 1