Bracketing Search

Hello, I'm very new to C++(you can see this is my first post here) and I'm doing a beginner exercise called Bracketing Search, I'm pretty sure that most of you already know this exercise, but to remind you if probably you forget, here's the exercise task :

Write a program that calculates a random number 1 through 100. The program then asks the user to guess the number.
If the user guesses too high or too low then the program should output "too high" or "too low" accordingly.
The program must let the user continue to guess until the user correctly guesses the number.

★ Modify the program to output how many guesses it took the user to correctly guess the right number.

★★ Modify the program so that instead of the user guessing a number the computer came up with, the computer guesses the number that the user has secretely decided. The user must tell the computer whether it guesed too high or too low.

until now I'm doing the second modify(the two stars), here's my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
using namespace std;

int main()
{
    int Min = 1,Max = 100;
    int UserSecretNumber,UserGuide;
    int ComputerInput = rand() % 100+1;
    int ComputerTries = 0;
    cout << "Enter your secret number between 1-100 ";
    cout << "\nPretend the computer is never know you've inputted your secret number here: ";
    cin >> UserSecretNumber; cout << endl;
        if (cin.fail())
        {cout << "You've inputed non numeric character, retry this application.\n"; getch(); return 0; }
        if (UserSecretNumber > 100 || UserSecretNumber < 1)
            {
                cout << "You are a dumb, I said 1-100" << endl;
                cout << "You must retry this application.";
                getch();
                return 0;
            }
    cout << "Now the computer will guess your secret number." << endl;
    cout << "You must help the computer by enter the guide number." << endl;
    cout << "If your secret number is bigger, enter number (1)" << endl;
    cout << "If your secret number is lower, enter number (2)" << endl;
    cout << "If it is correct, enter number (3)" << endl << endl;

    srand(time(NULL)); // I don't really know, is this srand necessary or not..

    cout << "Is your number: " << ComputerInput << " ?" << endl;


       do { ComputerTries++;
            cout << "Enter your inspection (1) or (2) or (3): ";
            cin >> UserGuide;
            if      (UserGuide == 1)
               {
                 Min = ComputerInput + 1;
                 ComputerInput =  rand() % (Max - (Min + 1)) + Min;
                 cout << "Is your number: " << ComputerInput << " ?" << endl;
               }
            else if (UserGuide == 2)
               {
                 Max = ComputerInput - 1;
                 ComputerInput = rand() % (Max - (Min + 1)) + Min;
                 cout << "Is your number: " << ComputerInput << " ?" << endl;
               }
            else if (UserGuide == 3) goto correct; // I dunno but I use this :p
            else // If the user input anything other than 1,2, and 3
             {
                 cout << "\nError Guide.\nPlease retry the application, " << endl;
                      getch();
                      return 0;
             }
          }
       while ("is this will ever be used ?"); // Is this while will ever be used ? Haha, I think not, I'm such a dumb -_-
  correct : cout << "\nSo the computer have guessed correct. \n";
            cout << "Your secret number is: " << ComputerInput;
            cout << ". It took the computer " << ComputerTries << " tries.\n\n";
            cout << "This only for learning purpose, thank you";
getch();
 return 0;
}


Okay,If you try this code, and test it, it seems look good and nice, but when you test and input your secret number with a number ,and then when the computer is very close at guessing it will crash, but if computer guessed correct without guess wrong number that is very close, the program works fine.

for example :
My secret number is 66, and then the computer guess 65, so I tell the computer that my number is bigger and then next the computer guess 67, then I tell that my number is lower, which means the next guess by the computer would be 66. That's the time when the program crash..
But let's say the computer first guess is 42, an then second is 76, the third guess is 66, the program will work nice..

I have googled everywhere and search everywhere, even youtube, and I don't get any clue, so can someone help me ? or give some explanation about this cursed number ?

P.S. : I'm a very newbie so I'm sorry If there is any mistake I've made in this post, thank you very much
closed account (zybCM4Gy)
line 51: use

 
break;


Using goto is sometimes necessary in heavily nested loops but usually a break will be satisfactory.


Scratch that. The while loop will always cycle round. It'll always be true. This is one reason I do not advocate do-while loops. There's never usually a reason that is good enough to use it.

Edit
I'll also point out that conio.h is none standard. At least... it's not working on linux ¬_¬;; So get char is out of the window.

You'll be glad to know the code works for me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main()
{
    int Min = 1,Max = 100;
    int UserSecretNumber,UserGuide;
    int ComputerInput = rand() % 100+1;
    int ComputerTries = 0;
    cout << "Enter your secret number between 1-100 ";
    cout << "\nPretend the computer is never know you've inputted your secret number here: ";
    cin >> UserSecretNumber; cout << endl;

    /*
     * I wouldn't use cin.fail() here I'd use if(userSecretNumber < 1 || userSecretNumber > 100)
     * I'd also wrap the error checking in a while loop of its own and instead of return 0. Simply 
     * cycle through the loop until the user gives an acceptable answer. 
     */
	if (cin.fail())
	{
		cout << "You've inputed non numeric character, retry this application.\n"; return 0;
	}
	if (UserSecretNumber > 100 || UserSecretNumber < 1)
	{
		cout << "You are a dumb, I said 1-100" << endl;
		cout << "You must retry this application.";
		return 0;
	}
    cout << "Now the computer will guess your secret number." << endl;
    cout << "You must help the computer by enter the guide number." << endl;
    cout << "If your secret number is bigger, enter number (1)" << endl;
    cout << "If your secret number is lower, enter number (2)" << endl;
    cout << "If it is correct, enter number (3)" << endl << endl;

    srand(time(NULL)); // I don't really know, is this srand necessary or not..

    cout << "Is your number: " << ComputerInput << " ?" << endl;


   do
   {
	   ComputerTries++;
	   cout << "Enter your inspection (1) or (2) or (3): ";
	   cin >> UserGuide;
	   if(UserGuide == 1)
       {
		   Min = ComputerInput + 1;
		   ComputerInput =  rand() % (Max - (Min + 1)) + Min;
		   cout << "Is your number: " << ComputerInput << " ?" << endl;
	   }
	   else if (UserGuide == 2)
	   {
		 Max = ComputerInput - 1;
		 ComputerInput = rand() % (Max - (Min + 1)) + Min;
		 cout << "Is your number: " << ComputerInput << " ?" << endl;
	   }
	   else if (UserGuide == 3) break; // I dunno but I use this :p
	   else // If the user input anything other than 1,2, and 3
	   {
		   cout << "\nError Guide.\nPlease retry the application, " << endl;
		   return 0;
	   }
   }while (UserGuide != 3); // Is this while will ever be used ? Haha, I think not, I'm such a dumb -_-
   cout << "\nSo the computer have guessed correct. \n";
   cout << "Your secret number is: " << ComputerInput;
   cout << ". It took the computer " << ComputerTries << " tries.\n\n";
   cout << "This only for learning purpose, thank you";
   return 0;
}
Last edited on
It crashes because you're dividing by 0.

Let's say Min is 64 and Max is 65.
Then (Max - (Min + 1)) is 0, so you'd be doing
ComputerInput = rand() % 0 + Min;.

In fact, your formula is actually incorrect; it should be
ComputerInput = rand() % (Max - Min + 1) + Min; // No parenthesis .
closed account (zybCM4Gy)
But bodmas applies doesn't it?

so (max - (Min +1)) is equivalent to (Max - Min + 1).
No, Max - (Min + 1) == Max + (-1)*(Min + 1) == Max + (-1)*Min + (-1)*(1) == Max - Min - 1.

(You forgot to distribute the negative sign.)
Last edited on
closed account (zybCM4Gy)
fuuuu....

God darn it he's right. I completely forgot about expanding brackets -____-;

Let me just book myself in for mathematics classes again.
Wow it worked fine, stupid of me and my formula..
Thank you Mr F0x for correcting my goto things, and thank you Mr long for warn me that my formula is incorrect and give me the correct formula..
^__^
But for Mr F0x, you suggest me not to use cin.fail(), can you explain it why ? is it forbidden or it's not going work in different place ? I really don't know about this I'm sorry..
My reason for using cin.fail() is because I want the program showing error if the user input non numeric character, and I don't know any method that effective beside the cin.fail(), so I will research and learn again about this matter. Best thank you.
Last edited on
I think he just meant that it would be better if, rather than quitting the application and forcing the user to restart the program if an invalid input is entered, just wrap it in a while loop and loop until the user enters something valid:
1
2
3
4
5
6
7
8
cout << "\nPretend the computer is never know you've inputted your secret number here: ";
while (!(cin >> UserSecretNumber) || (UserSecretNumber > 100 || UserSecretNumber < 1))
{
    cin.clear(); // Clear any error flags
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // Clear input stream
                                                         // (requires #include <limits>)
    cout << "You inputted something invalid, try again: ";
}
Ah, I see.. yeah maybe rather than restart the program, it's more convenient for the user if I make it loop, because if user accidentaly missed type, he/she will frustrated for restarting the program..
thank you Mr Long for your explanation about Mr f0x's opinion, I will make some change in my code..

I'm sorry if you don't mind, I want to ask you about the argument inside the while,
the >> !(cin >> UserSecretNumber) << would you explain a little about this ?

Yes I know I'm such a dumb for asking such silly question.. But really, I dont' know the whole process in there, (Oh my God how can I explain this, hope you know what I mean, very sorry)
, I just know that it has something to do with give invalid output if the user type non numeric characters, or it is the same like cin.fail(), because UserSecretNumber is an integer type data, so if the user input non integer, it will goes Blarrr..!! ?? like that ?

Best thank you for you God Bless
Topic archived. No new replies allowed.