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