Guessing Game

Create a C++ console application that generates a random number that m

Page 1 of 1

12 Replies - 1848 Views - Last Post: 20 September 2009 - 11:43 PM Rate Topic: -----

#1 mabaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Guessing Game

Post icon  Posted 20 September 2009 - 09:51 AM

Create a C++ console application that generates a random number that must be guessed by a user. The program will indicate to the user if their guess is correct, too high or too low. It will also report the number of guesses the user has made. The game ends when the user enters a negative one (-1) or has guessed the number. The program should include a do while loop, decision statements, constants and variables.

Here is what I have but it does not work properly.....what am I doing wrong?

 #include <iostream>
#include <time.h>

using namespace std;

int main(void)
{
	srand(static_cast<int> (time(NULL)));

	int upperLimit = 10;
	int lowerLimit = 1;
	int userGuess = 0;
	int count = 0;
	int randNum = 0;
	const int exit = -1;

	cout << "***Welcome to the Guessing Game!***\n";
	srand (int(time(NULL)));
	randNum = (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;
	cout << "Pick a number between 1-10 (-1 to Exit): ";
	cin >> userGuess;
	
	do
	{ 		
		if (userGuess < randNum)
			cout << endl << "Too low, try again." << endl;
		else if (userGuess > randNum)
			cout << endl << "Too high, try again." << endl;
				
		else if (userGuess == randNum)
			cout << endl << "You've guessed wisely" << endl;
		else
		cout << "The number was " << randNum << " you gave up after " << count << " guesses.\n";	
		count++;
		cin >> userGuess;	
	}
	while(userGuess != randNum && userGuess > 0);
	
	
	cout << "The number was " << randNum << " it took you " << count << " guesses.\n";
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Guessing Game

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Guessing Game

Posted 20 September 2009 - 09:56 AM

I would put the input in the do while loop so the user can continue playing.
Was This Post Helpful? 1
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Guessing Game

Posted 20 September 2009 - 09:58 AM

http://www.dreaminco...h...st&p=773393 :)
Was This Post Helpful? 0
  • +
  • -

#4 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Guessing Game

Posted 20 September 2009 - 10:00 AM

I agree with eker676. Other that that i see no error? How are you expecting the code to behave?
Was This Post Helpful? 0
  • +
  • -

#5 mabaton  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 09

Re: Guessing Game

Posted 20 September 2009 - 10:31 AM

View Postsnowflakes, on 20 Sep, 2009 - 09:00 AM, said:

I agree with eker676. Other that that i see no error? How are you expecting the code to behave?

Here is my updated code. Everything works except the count when the number is guessed correctly....its one less than it should be. But the count is correct if I choose -1 to exit after a few guesses. Not sure how to fix it. When I put the count at the top of the loop it throws off both counts ( -1 and guessing correctly)

#include <iostream>
#include <time.h>

using namespace std;

int main(void)
{
	srand(static_cast<int> (time(NULL)));

	int upperLimit = 10;
	int lowerLimit = 1;
	int userGuess = 0;
	int count = 0;
	int randNum = 0;
	const int exit = -1;

	cout << "***Welcome to the Guessing Game!***\n";
	srand (int(time(NULL)));
	randNum = (rand() % (upperLimit - lowerLimit + 1)) + lowerLimit;
	
	
	do
	{ 	
		
		cout << "Pick a number between 1-10 (-1 to Exit): ";
		cin >> userGuess;	
		if (userGuess < randNum && userGuess != exit)
			cout << endl << "Too low, try again." << endl;
		else if (userGuess > randNum)
			cout << endl << "Too high, try again." << endl;			
		else if (userGuess == randNum)
		cout << endl << "You've guessed wisely" << endl 
			<< "The number was " << randNum << " it took you " << count << " guesses.\n";
		else 
			cout << "The number was " << randNum << " you gave up after " << count << " guesses.\n";
		count++;	
	}
	while(userGuess != randNum && userGuess > 0);
		
	
	
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#6 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Guessing Game

Posted 20 September 2009 - 10:44 AM

instead of using
do
{//your code here}
while(userGuess != randNum && userGuess > 0);



try
while(userGuess != randNum && userGuess > 0)
{//your code here
};


This post has been edited by aks29921: 20 September 2009 - 11:34 PM

Was This Post Helpful? 0
  • +
  • -

#7 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Guessing Game

Posted 20 September 2009 - 10:46 AM

No, don't. do is the better option since you always want to run the code at least once. Just increment it again before you output it.

count++;

Or start from 1 instead of 0. :)

This post has been edited by gabehabe: 20 September 2009 - 10:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Guessing Game

Posted 20 September 2009 - 10:55 AM

Cant it be changed to ++count; i think it would work.
Was This Post Helpful? 0
  • +
  • -

#9 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Guessing Game

Posted 20 September 2009 - 11:03 AM

View Postgabehabe, on 20 Sep, 2009 - 09:46 AM, said:

No, don't. do is the better option since you always want to run the code at least once. Just increment it again before you output it.

count++;

Or start from 1 instead of 0. :)


he is getting the correct count when he is exiting by pressing -1....

i think i have got the exact problem....

in do-while, the code runs first and then the count increments.
so, when the user guesses correctly the count that is getting printed is not updated at the last step (the step when the user guesses correctly) and this is happening all the time.
however on pressing -1, then there is no need of the count getting updated hence the correct answer..

the above problem can be removed by:
cout << endl << "You've guessed wisely" << endl
			<< "The number was " << randNum << " it took you " << count+1 << " guesses.\n";
 

This post has been edited by aks29921: 20 September 2009 - 11:43 PM

Was This Post Helpful? 0
  • +
  • -

#10 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Guessing Game

Posted 20 September 2009 - 11:09 AM

The count increments in the do loop.

Just initialise count to = 1 instead of 0 and that'll sort it.
Was This Post Helpful? 0
  • +
  • -

#11 AGRAC393  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 248
  • Joined: 13-May 09

Re: Guessing Game

Posted 20 September 2009 - 03:08 PM

ha there's another thread with the same question by different people... Same class?
Was This Post Helpful? 0
  • +
  • -

#12 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: Guessing Game

Posted 20 September 2009 - 06:53 PM

I still think that the ++count will work fine f3
Was This Post Helpful? 0
  • +
  • -

#13 aks29921  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 84
  • View blog
  • Posts: 230
  • Joined: 24-August 09

Re: Guessing Game

Posted 20 September 2009 - 11:43 PM

++count won't work as it is not the problem of prefix and postfix operators changing the count just after an operation, rather its the problem of count getting updated in the loop..

hence as mentioned in my above post, replacing count by count+1 at that one place will do the trick and the game will work f9.....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1