Jump to content

guess the number problem

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
CommittedC0der

CommittedC0der

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,565 posts
I have a problem with guess the number I am using the book learning c++ through game programing. What happens is the player and the computer take turns guessing the number. I want to make the computer narrow down the numbers intel it gets the answer not just randomly pick a number.
the problem here is it makes a random number for the first guess but then it just chooses that number over and over instead of picking a new number between comguessHigh and Low.
here's my code so far.


#include<iostream>

#include<cstdlib>

#include<ctime>


using namespace std;


int main()

{

	

	

	

	char again = 'y';

		while (again == 'y')

		{

	srand(time(0));

    

	int comguessHigh = 100;

    int comguessLow = 1;

	int thenumber = rand() % 100 + 1;

	int tries = 0, guess;

   

	do

	{

	cout << "enter a number." << endl;

	cin >> guess;

	tries += 1;


	if (guess < thenumber)

	{

		cout << "\nthats to low.\n" << endl;

	

	int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow;

	cout << "\nComperter guesses " << comguess << endl;

	

	

	if (comguess < thenumber)

		cout << "\ncomputers guess was to low.\n" << endl;

	    comguessLow = comguess;

	

	if (comguess > thenumber)

		cout << "\ncomputers guess was to high.\n" << endl;

	    comguessHigh = comguess;


    if (comguess == thenumber)

		cout << "\nSorry the computer guessed it." << endl;

    }

	

	if (guess > thenumber)

	{

		cout << "\nthats to high." << endl;


			int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow;

	        cout << "\nCompurter guesses " << comguess << endl;

	

	if (comguess < thenumber)

	    {

		cout << "\ncomputers guess was to low.\n" << endl;

	    comguessLow = comguess;

	    }

	

	if (comguess > thenumber)

	    {

		cout << "\ncomputers guess was to high.\n" << endl;

	    comguessHigh = comguess;

	    }

	

		

		

	}


	

	}while(guess != thenumber);

		cout << "thats it you guessed it in " << tries << " tries." << endl;


		cout << "\n Do you want to play again? (y/n)" << endl;

		cin >> again;

		}

		cout << "\nOk mabye next time.\n" << endl;


		system ("PAUSE");

	return 0;

}




please help thank you.

I tried putting in the code from jas12745 and it worked one time so I shut it off and started it back up and now it picks a number and won't change it again. I don't get it?

I now this is probly stupid but I just started playing with c++ like a week ago should I click the debug button to test my programs or is there a better way?

Edited by CommittedC0der, 14 November 2009 - 09:17 AM.
Use the [code] tags.


#2
jas12745

jas12745

    Newbie

  • Members
  • Pip
  • 3 posts
Hey i dont get your point, but so far i know to solve the problem that gets the same number you have to use this..:
(ADD IT ACTUALLY)

#include <time.h>

#include <stdlib.h>

int main(){

srand(time(NULL));

//then you can use the rand() funtion with different numebers each call

return 0;

}



#3
jas12745

jas12745

    Newbie

  • Members
  • Pip
  • 3 posts
Ok..!
You have a problem... your program is a mess; first you have to check what you are using... Here this is your code modified (didnt mean to but i did it) interesting game. You have to look for the places you start and close the conditionals; furthermore, you have to look how many times you've written the same code..(if clauses) Also check the condition to get out of the loop; i dint want to change it, but when the computers guessed it it never ended.
This is just one way to solve it, you can create your own (I based on your code). have fun.
PD: You could take a look to the book How to program in C/C++ Its really helpful (read Theory, your book is for practice).

#include<iostream>

#include<cstdlib>

#include<ctime>

#include <stdlib.h>

using namespace std;


int main()

{

	

	

	char who;

	char again = 'y';

		while (again == 'y')

		{

	srand(time(NULL));

    

	int comguessHigh = 100;

    int comguessLow = 1;

	int thenumber = rand() % 100 + 1;

	int tries = 0, guess;

   

	do

	{

	cout << "enter a number." << endl;

	cin >> guess;

	tries += 1;


	if (guess < thenumber)

	{

		cout << "\nthats to low.\n" << endl;

    }

	int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow;

	cout << "\nCompurter guesses " << comguess << endl;

	

	

	if (comguess < thenumber)

	{	cout << "\ncomputers guess was to low.\n" << endl;

	    comguessLow = comguess;

     }

	if (comguess > thenumber)

    {	cout << "\ncomputers guess was to high.\n" << endl;

	    comguessHigh = comguess;

     }

    if (comguess == thenumber)

    {	cout << "\nSorry the computer guessed it." << endl;guess=thenumber;who='c';

    }

	if (guess > thenumber)

	{

		cout << "\nthats to high." << endl;


			int comguess = rand() % (comguessHigh - comguessLow + 1) + comguessLow;

	        cout << "\nCompurter guesses " << comguess << endl;

         }	

	if (comguess < thenumber)

	    {

		cout << "\ncomputers guess was to low.\n" << endl;

	    comguessLow = comguess;

	    }

	

	if (comguess > thenumber)

	    {

		cout << "\ncomputers guess was to high.\n" << endl;

	    comguessHigh = comguess;

	    }

	

		

     cout<<endl<<thenumber;

	

	}while(guess != thenumber);

        if (who!='c'){

		cout << "thats it you guessed it in " << tries << " tries." << endl;

    }

		cout << "\n Do you want to play again? (y/n)" << endl;

		cin >> again;

		}

		cout << "\nOk mabye next time.\n" << endl;


		system ("PAUSE");

	return 0;

}



Check out your code again...