Hello every one I'm trying to write a game that has a person guess a number between 1 and 100 hand have the computer guess that number via inputs from the user but I keep running into snags.

#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int main()
{
        srand ((unsigned)time(0));
        int num, x, y, z;
        char ans, ans2;
        cout<<"Please pick a number between 1 and 100, are you ready \n";
        cout<<"to begin? y or n"<<endl;
        cin>>ans;

        if (ans!='n')
        {
                do
                {
                y=100;
                z=1;
                x= rand () % y + z;
                cout<<"Is your number higher or lower then "<<x<<" or neither."<<endl;
                cin>>ans;
                if(ans=='h')
                        {
                        z = z -(y-x);
                        y = x;
                        }
                if (ans=='l')
                        z=x-z;
                }while (ans!='n');
        cout<<"Is your number: "<<x<<endl;
        }
return 0;
}

I'm trying to have the PRNG narrow down its search perimeters with every entry from the user but every time I change something and think that I have fixed the problem the game does something that is unexpected.... it is driving me batty any help would be much appreciated.

Hello. Why is your program generating a random number? It's the human who has to think of one! You can take that bit out.

You need to use the idea of a 'number line'. Say you need to choose a number between 1 - 10 and you choose 7. At the start of the program the upper limit is 10 and the lower is 1. Your program needs to reduce this scope, by asking first if your chosen number <= 10 - you answer lower. So now your program should take the half-way point, 5, and asks if this is <> or = to your chosen number - you answer higher. Now the program knows that the upper limit is 10 and the lower is 5.

This 'halving' is repeated until there is only one number left.
Hope this helps a little bit!

i think hes trying to make the program generate a random number and have the user guess it.

or something like 20 questions

srand ((unsigned)time(0));

you need to use #include <ctime>
to get time.
also you might want to generate numbers based on the users choice, like:
is your number more than 5?
(user chooses yes)
is you number less than 10?
(user chooses yes)

then the program should think that the number is between 10 and 5.

because in your program i chose higher than 50 and it asked me if my number was 1.
use less than and more than or equalto signs in your if functions.

also you dont want to initialize x and y integers in the loop. everytime it does the loop those integers will be reset to 1 and 100, giving you no baseline

i played around with it..

#include <iostream>
    #include <cmath>
    #include <cstdlib>
    #include <ctime>  /you need this to declare time
    using namespace std;

    int main()
    {
    srand ((unsigned)time(0));
 char yn;
 int parameter = 0;
 int x = 0;
 int rar = 100; //your guesses
 int ror = 1;
 int counter = 0; //counts how many tries it took me to get the answer
 char number = 0;
while (yn != 'q')
{
    x= rand () %  rar+ror ;
cout << "is your number lower than " << x <<"?" << "guessed " <<counter <<"times " << "\npress q if i guessed your number correctly " << "(y/n) for answers\n";
cin >> yn;
if (yn == 'y') //if ur answer is yes that your number is lower
rar = x;


else           /if you number is no
ror = x;

rar = rar - ror; //gets the numbers in between to guess

counter ++;
}

return 0;
    }

it will guess it in about 30 tries or so.
it needs more tweaking though.
sometimes it will get really close to the number (but thats rand for you) and then go completely random then it will come back close again.

it has a hard time guessing 69 for some reason
it also has a fu.ck detector, it will terminate itself if it senses that youre screwing around with it (lets just say, dont choose negative numbers).
also dont choose numbers higher than 1,000 because theres a lot of whole numbers there and it will take a while to guess it and ma trip the fu.cck detector.

This is something i came up with where it guesses in ten or less trys.

#include "stdafx.h"
#include <iostream>
#include <ctime>

using std::cout;
using std::endl;
using std::cin;

int high_low(int,int,int);

int main()
{
	srand(int(time(NULL)));
	int random = 1 + rand() % 1000;
	int guess = 1000, counter = 0;
	int low = 0, high = 0;

	cout << "Random #" << random << endl;

	while( guess != random && counter++ < 10)
	{
		cout << "Guess # " << counter << " number: " << guess << endl;
		
		if( guess > random )
		{
			cout << "Lower...\n";
			high = guess;
			guess = high_low(guess,high,low) + low;
		}
		else
		{
			cout << "Higher...\n";
			low = guess;
			guess += high_low(guess,high,low);
		}
	}
	cout << "Number you guessed is: " << guess << " in "
		 << counter << " tries";
	
	cin.get();
	cin.get();
	return 0;
}

int high_low(int guess,int high,int low)
{
	return (high - low) / 2;
}

sorry i misread your post i thought you wanted computer to guess a random number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.