User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,563 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,508 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1417 | Replies: 7
Reply
Join Date: Dec 2006
Posts: 7
Reputation: GuruGhulab is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
GuruGhulab GuruGhulab is offline Offline
Newbie Poster

Guessing game

  #1  
Jan 29th, 2007
Hi there, I need to create a program that generates a random number, and asks the user to guess the number and the program tells them higher or lower, and if the program tells them higher, and they enter a number lower, or vice-versa, you call them a moron. For example,

Random number= 34
User enters- 17
Program says- higher
User enters- 10
Program says- moron higher

You must count the total number of guesses, and the number of time they were called moron.

I need help with the Moron counter, Thanks.

#include <iostream>
#include <time.h>
using namespace std;

int main ()
{
	int nGuess,nNum,nCount=0;
	srand((unsigned)time(0));
	nGuess=rand ()%50+1;
	while (nNum!=nGuess)
	{
		cout<<"Please enter a number between 1-50"<<endl;
		cin>>nNum;
		if (nNum==nGuess)
		{
			cout<<"Correct"<<endl;
			nCount+=1;
		}
		else if (nNum>nGuess)
		{
			cout<<"Lower"<<endl;
			nCount+=1;
		}
		else if (nNum<nGuess)
		{
			cout<<"Higher"<<endl;
			nCount+=1;
		}
	}
	cout<<"Total gueses equal "<<nCount<<endl;
	//cout<<"Total Moron's equal "<<MoronCount<<endl;
	return 0;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Guessing game

  #2  
Jan 29th, 2007
you need to add two more counters -- total number of morons and another counter to indicate whether the last guess was higher or lower.
For example:
else if (nNum>nGuess)
{
    if( last_guess < nGuess )
    {
          cout << "moron";
          moronCoujnt++;
    }
}
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,535
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 283
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Guessing game

  #3  
Jan 29th, 2007
You need to keep track of whether or not you've already told the user whether he/she needs to go higher or lower - perhaps store it in an int; 0 means no instruction yet, 1 means higher, and -1 means lower.

Test this int value before you tell the user whether to go higher or lower, and then if it's correct reset the variable and print out the hint. If it's wrong, call them a moron and increment the moron counter.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Dec 2006
Posts: 7
Reputation: GuruGhulab is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
GuruGhulab GuruGhulab is offline Offline
Newbie Poster

Re: Guessing game

  #4  
Jan 29th, 2007
Originally Posted by Ancient Dragon View Post
you need to add two more counters -- total number of morons and another counter to indicate whether the last guess was higher or lower.
For example:
else if (nNum>nGuess)
{
    if( last_guess < nGuess )
    {
          cout << "moron";
          moronCoujnt++;
    }
}


i need help with this part

counter to indicate whether the last guess was higher or lower.
For example:
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,535
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 283
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Guessing game

  #5  
Jan 29th, 2007
Originally Posted by GuruGhulab View Post
i need help with this part
Hmm... try what I suggested with values of -1, 0 and 1.

For example:
enum {lower_guess=-1, no_guess, higher_guess};
int prev_guess; // use this for keeping track of previous guess

Then if the guess needs to be higher, and prev_guess is 0, then set prev_guess to higher_guess, if the guess was lower, than set it to lower_guess, etc..

The trickier part is detecting morons :mrgreen:. If the value of your variable for keeping track of prev_guess is not 0, you need to check that the value is higher_guess if the guess needs to be higher, and lower if it's lower_guess. If it's not, call the user a moron and increment the moron counter.

Hope that made sense.
Last edited by John A : Jan 29th, 2007 at 10:58 pm.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Guessing game

  #6  
Jan 29th, 2007
Originally Posted by GuruGhulab View Post
i need help with this part


I posted the answer to one half. Just copy it into the other half and make appropriate changes to the less-than-operator. I know you can do it if you think about it a few minutes. If you have been racking your brain several hours, then go watch TV or do something else for awhile.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,535
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 17
Solved Threads: 283
Moderator
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: Guessing game

  #7  
Jan 29th, 2007
Originally Posted by Ancient Dragon View Post
I posted the answer to one half. Just copy it into the other half and make appropriate changes to the less-than-operator. I know you can do it if you think about it a few minutes. If you have been racking your brain several hours, then go watch TV or do something else for awhile.
Oh sorry, Ancient Dragon! My solution has way too much fluff; your method is much better.

Nevermind my bloated-and-overly-complicated code solution.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Guessing game

  #8  
Jan 29th, 2007
Originally Posted by joeprogrammer View Post
Oh sorry, Ancient Dragon! My solution has way too much fluff; your method is much better.

Nevermind my bloated-and-overly-complicated code solution.

You had a good solution too. Don't discount the power of multiple suggestions.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC