I am having a little trouble with guess_my_number program. how do i let the computer guess my number? instead of me guessing the number

// Guess My Number

// The classic number guessing game

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

srand(static_cast <unsigned int> (time(0))); // seed random number generator

int secretNumber = rand() % 5 + 1; // random number between 1 and 100

int tries = 0;

int guess;

cout << "\t\t\tWelcome to Guess My Number\n\n";

do

{

cout << "Enter a guess:";

cin >> guess;

++tries;

if (guess > secretNumber)

{

cout << "Too high!\n\n";

}

else if ( guess < secretNumber)

{

cout << " Too low!\n\n";

}

else

{

cout << "\nThat's it! You got it in " << tries << " guesses!\n";

}

} while (guess != secretNumber);

return 0;

}

// Guess My Number

// The classic number guessing game

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main()

{

srand(static_cast <unsigned int> (time(0))); // seed random number generator

int secretNumber = rand() % 5 + 1; // random number between 1 and 100

int tries = 0;

int guess;

cout << "\t\t\tWelcome to Guess My Number\n\n";

do

{

cout << "Enter a guess:";

cin >> guess;

++tries;

if (guess > secretNumber)

{

cout << "Too high!\n\n";

}

else if ( guess < secretNumber)

{

cout << " Too low!\n\n";

}

else

{

cout << "\nThat's it! You got it in " << tries << " guesses!\n";

}

} while (guess != secretNumber);

return 0;

}

I would start by changing the gap between the number the computer is guessing. For example, if the number is 50 and the gap is 0 to 100 and the computer picks 22, the random generator the computer is picking from should change the gap to 22 to 100.Your if statements should have a modifier of the randim number gap from the computer. Im reletively new to programming, so if anyone else wants to comment on mine feel free, i learn through helping. :-)

Well you have to sort out an algorithm!!! The most fair thing to do is to divide the interval in two parts, so, if the interval is 1 -100, you ask the user: "is 50?" and then he can answer "yes" or "smaller" or "bigger".. then you divide the interval 0-49 or 51-100 in two parts and so and so, in max 7 times you arrive at the solution.

Topic archived. No new replies allowed.